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:
private
List<SpringPersistenceUnitInfo> readPersistenceUnitInfos() {
List<SpringPersistenceUnitInfo> infos =
new
LinkedList<SpringPersistenceUnitInfo>();
boolean buildDefaultUnit = (
this
.packagesToScan !=
null
||
this
.mappingResources !=
null
);
PersistenceUnitReader reader =
new
PersistenceUnitReader(
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());
}
return
infos;
}
|
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