??
在Spring 集成 Hibernate 的JPA方式中,须要在persistence配置文件里定义每个实体类。这样很地不方便。2种方法能够解决此问题:
这2种方式都能够实现不用在persistence.xml文件里配置每个实体类,从而免去每个Entity都要在persistence.xml文件里配置的烦恼,可是这样的方式Entity实体类的主键字段注解@ID要放到 getXXX()方法上。否则不认。
方式1:
改动“LocalContainerEntityManagerFactoryBean”的配置,例如以下:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="packagesToScan" value="com.sunitjy.model.entityName" />
<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>
<prop key="hibernate.connection.username">${jdbc.username}</prop>
<prop key="hibernate.connection.password">${jdbc.password}</prop>
<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>
方式1没有使用 persistence 这个配置文件。注意咯!
方式2:
改动“LocalContainerEntityManagerFactoryBean”的配置,例如以下:
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<!-- <property name="persistenceUnitName" value="pro_persistence"></property>-->
<property name="dataSource" ref="dataSource"></property>
<property name="persistenceXmlLocation" value="classpath*:pro_core/jpa_persistence.xml"></property>
<property name="packagesToScan">
<list>
<value>com.paic.lfex.model</value>
<value>com.lfex.sdp.core.model</value>
<value>com.paic.lfex.core.pro</value>
</list>
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
</bean>
persistence.xml配置文件内容:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="pro_persistence"
transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="current_session_context_class" value="thread" />
<!--<property name="hibernate.hbm2ddl.auto" value="update" /> -->
<!--<property name="hibernate.show_sql" value="true" />-->
<!--<property name="hibernate.format_sql" value="true" />-->
</properties>
</persistence-unit>
</persistence>
方式2使用了 persistence 配置文件,去掉“persistenceUnitName”属性,加入“packagesToScan”属性。persistence.xml配置文件里的persistence-unit名字照样保留。可是 persistence 配置文件里不须要对实体类进行配置,会自己主动识别。
为什么去掉“persistenceUnitName”属性就能够自己主动识别实体了呢?看一下Spring的源代码就知道了:
类名:org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager
代码段:
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;
}
|
注意看这个源代码的方法,defaultPersistenceUnitName 变量假设不为空。而且等于 persistence 配置文件里的持久化单元名称,则buildDefaultUnit就为false,buildDefaultUnit 假设为 false,是不会运行 buildDefaultPersistenceUnitInfo() 方法的,而 buildDefaultPersistenceUnitInfo() 方法是依据我们定义的 packagesToScan 去自己主动扫描Entity实体类的。
注:我使用的是 Spring 3.2.3
以上2种方法都測试通过。