JPA does not configure 2 solutions for each entity class in the Persistence.xml file

Source: Internet
Author: User

Original address: http://www.cnblogs.com/taven/p/3351841.html

JPA does not configure 2 solutions per Entity entity class in the Persistence.xml file in the JPA mode of spring integrated Hibernate, it is very inconvenient to define each entity class in the persistence configuration file, Far elder brother now found 2 ways. These 2 ways can be implemented without persistence.xml files, eliminating each entity to be configured in the Persistence.xml file, but this way the entity entity class primary key field annotation @id to be put on the GetXXX () method, otherwise not recognized.

Mode 1: Modify the configuration of "Localcontainerentitymanagerfactorybean" as follows: <bean id= "Entitymanagerfactory" class= " Org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean "><property name=" Packagestoscan "value= "Com.pilicat.data.entity"/><property name= "Jpavendoradapter" ><bean class= " Org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter "/></property><property name=" Jpaproperties >            <props>                <prop key= "Hibernate.connection.driver_class" >${jdbc.driverClassName}</prop>                <prop key= "Hibernate.connection.url" >${jdbc.url}</prop>  & nbsp             <prop key= "Hibernate.connection.username" >${jdbc.username}</prop >                <prop key= "Hibernate.connection.password" >${ jdbc.password}</prop>            &NBSp   <prop key= "hibernate.c3p0.min_size" >10</prop>                <prop key= "Hibernate.hbm2ddl.auto" >true</prop>                < Prop key= "Hibernate.dialect" >${hibernate.dialect}</prop>            </props >        </property></bean>  mode 1 does not use the  persistence configuration file, note!    Mode 2: Modify the configuration of "Localcontainerentitymanagerfactorybean" as follows:<bean id= "Entitymanagerfactory" class= "Org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" ><property name= "DataSource" ref= "DataSource"/><property name= "persistencexmllocation" value= " Classpath:persistence-app.xml "/><!--<property name=" Persistenceunitname "value=" PILICAT_APP_JPA "/>-- ><property name= "Packagestoscan" value= "com.pilicat.data.entity"/><property name= "Jpavendoradapter" ><bean class= "Org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" ><property name= " Databaseplatform "value=" ${hibernate.dialect} "/><property name=" Generateddl "value=" false "/></bean ></property> </bean> persistence-app.xml configuration file contents:

<?xml version= "1.0" encoding= "UTF-8"?
<persistence version= "2.0" xmlns= "Http://java.sun.com/xml/ns /persistence "&NBSP;
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "&NBSP;
xsi:schemalocation=" http ://java.sun.com/xml/ns/persistence 
Http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd ">

<persistence-unit name= "PILICAT_APP_JPA" transaction-type= "resource_local";

<properties
<property name= "hibernate.max_fetch_depth" value= "3"/>
<property name= "Hibernate.jdbc.fetch_ Size "value="/>
<property name= "hibernate.jdbc.batch_size" value= "/>";
<property name= " Hibernate.show_sql "value=" true "/>
<property name=" Hibernate.format_sql "value=" false "/>
</ Properties>
</persistence-unit>

</persistence>

Mode 2 uses the persistence configuration file, removes the "persistenceunitname" attribute, adds the "Packagestoscan" property, The persistence-unit name in the Persistence.xml configuration file remains, but the entity class is not required to be configured in the persistence configuration file and is automatically recognized. Why is it that the "Persistenceunitname" attribute is removed to automatically identify the entity? Take a look at spring's source code to know: Class Name: Org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager snippet:
privateList<SpringPersistenceUnitInfo> readPersistenceUnitInfos() {    List<SpringPersistenceUnitInfo> infos = newLinkedList<SpringPersistenceUnitInfo>();    boolean buildDefaultUnit = (this.packagesToScan != null|| this.mappingResources != null);    PersistenceUnitReader reader = newPersistenceUnitReader(this.resourcePatternResolver, this.dataSourceLookup);    SpringPersistenceUnitInfo[] readInfos = reader.readPersistenceUnitInfos(this.persistenceXmlLocations);    for(SpringPersistenceUnitInfo readInfo : readInfos) {        infos.add(readInfo);        if(this.defaultPersistenceUnitName != null&&                this.defaultPersistenceUnitName.equals(readInfo.getPersistenceUnitName())) {            buildDefaultUnit = false;        }    }    if(buildDefaultUnit) {        infos.add(buildDefaultPersistenceUnitInfo());    }    returninfos;}

Note that the source method, if the defaultpersistenceunitname variable is not empty, and equals the persistent unit name in the persistence configuration file, then the Builddefaultunit is false, Builddefaultunit If False, the Builddefaultpersistenceunitinfo () method is not executed, and Builddefaultpersistenceunitinfo () The method is to automatically scan entity entity classes According to our defined Packagestoscan.

Note: I am using Spring 3.2. More than 4 of the 2 methods are tested, is there any simpler way? You can also be your way to tell far elder brother, or in the far elder brother's blog under the message, welcome everyone to exchange and share, thank you.

JPA does not configure 2 solutions for each entity class in the Persistence.xml file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.