在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种方法都测试通过。
分享到:
相关推荐
最基础的JPA编程所需jar文件 附带mysql jar文件,persistence.xml配置文件
spring与jpa整合 除去persistence.xml配置文件 使用属性文件 数据源dbcp访问数据库
springMVC+JAP整合彻底摆脱persistence.xml配置文件 spring与jpa整合 除去persistence.xml配置文件 使用属性文件 数据源dbcp访问数据库.
这是一个javax.persistence.Entity 的jar包文件。有时候创建JPA工程导入EclipseLink库的时候会缺少这种文件,可以选择在库中加入这个jar包即可
spring+jpa的applicationContext.xml配置
Apress.Pro.JPA.2.in.Java.EE.8.An.In-Depth.Guide.to.Java.Persistence.APIs.3rd.Edition.
吉帕与 Guice 一起使用的 Jpa 模块,为所有用 @Entity 注释的类提供泛型类型安全的 DAO。 这仍然是一个 WIP 模块。 当前特点: 与 Guice 集成,为所有 @Entity 类提供 Typed Daos 的绑定。 #用法您可以签出 repo 并...
个人博客系统(SpringBoot+jpa).zip个人博客系统(SpringBoot+jpa).zip个人博客系统(SpringBoot+jpa).zip个人博客系统(SpringBoot+jpa).zip个人博客系统(SpringBoot+jpa).zip个人博客系统(SpringBoot+jpa)...
jar包,官方版本,自测可用
NULL 博文链接:https://abstractforever.iteye.com/blog/642219
EclipseLink JPA org.eclipse.persistence/org.eclipse.persistence.jpa/2.6.5/org.eclipse.persistence.jpa-2.6.5.jar
spring-data-simpledb.zip,根据spring数据接口提供以pojo为中心的模型以与amazon simpledb交互,非关系数据存储根据spring数据接口提供以pojo为中心的模型以与amazonsimpledb(非关系数据存储)交互
EclipseLink JPA org.eclipse.persistence/org.eclipse.persistence.jpa/2.6.6/org.eclipse.persistence.jpa-2.6.6.jar
EclipseLink JPA org.eclipse.persistence/org.eclipse.persistence.jpa/2.5.1/org.eclipse.persistence.jpa-2.5.1.jar
EclipseLink JPA org.eclipse.persistence/org.eclipse.persistence.jpa/2.6.9/org.eclipse.persistence.jpa-2.6.9.jar
EclipseLink JPA org.eclipse.persistence/org.eclipse.persistence.jpa/2.7.8/org.eclipse.persistence.jpa-2.7.8.jar
ejb-persistence.jar JPA注解
EclipseLink JPA org.eclipse.persistence/org.eclipse.persistence.jpa/2.6.2/org.eclipse.persistence.jpa-2.6.2.jar
jboss-web.xml jndi.properties oracle-ds.xml三个文件,EJB+JPA 所需要的一些配置文件
JPA注解.doc JPA注解.doc JPA注解.doc JPA注解.doc JPA注解.doc JPA注解.doc JPA注解.doc JPA注解.doc JPA注解.doc JPA注解.doc JPA注解.doc JPA注解.doc JPA注解.doc JPA注解.doc JPA注解.doc JPA注解.doc JPA注解....