在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.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>
                <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="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配置文件内容:
 

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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_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="50" />
<property name="hibernate.jdbc.batch_size" value="50" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="false" />
</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.4
 
以上2种方法都测试通过,还有没有更简单的办法呢?你也可以将您的方式告诉远哥,或在远哥的博客下方留言,欢迎大家交流分享,谢谢。
 
 
 

JPA 不在 persistence.xml 文件中配置每个Entity实体类的2种解决办法的更多相关文章

  1. 不在JPA 的 persistence.xml 文件中配置Entity class的解决办法

    在Spring 集成 Hibernate 的JPA方式中,需要在persistence配置文件中定义每一个实体类,这样非常地不方便,2种方法可以解决此问题: 这2种方式都可以实现不用在persiste ...

  2. 不在JPA 的 persistence.xml 文件里配置Entity class的解决的方法

     在Spring 集成 Hibernate 的JPA方式中,须要在persistence配置文件里定义每个实体类.这样很地不方便.2种方法能够解决此问题: 这2种方式都能够实现不用在persist ...

  3. Spring MVC框架下在java代码中访问applicationContext.xml文件中配置的文件(可以用于读取配置文件内容)

    <bean id="propertyConfigurer" class="com.****.framework.core.SpringPropertiesUtil& ...

  4. struts.xml文件中配置tiles.xml

    Apache Tiles是一个JavaEE应用的页面布局框架.Tiles框架提供了一种模板机制,可以为某一类页面定义一个通用的模板,该模板定义了页面的整体布局.布局由可以复用的多个块组成,每个页面可以 ...

  5. Mybatis自动生成xml文件、dao接口、实体类

    Mybatis可以通过逆向工程,实现自动生成xml文件.dao接口.实体类 以下使用的是Intellij Idea进行自动生成 一.首先,要在pom.xml中导入插件,在<build>中加 ...

  6. web.xml文件中配置ShallowEtagHeaderFilter需注意的问题

    问题现象及解决方法 今天在Spring MVC应用中加入ShallowEtagHeaderFilter时,发现返回的响应中没有etag头,上网查了很多相关资料,也试了很多方法依然不起作用.在查看web ...

  7. Spring 在xml文件中配置Bean

    Spring容器是一个大工厂,负责创建.管理所有的Bean. Spring容器支持2种格式的配置文件:xml文件.properties文件,最常用的是xml文件. Bean在xml文件中的配置 < ...

  8. xml文件中配置JDBC源遇到问题 : The reference to entity "characterEncoding" must end with the ';' delimiter

    数据源配置时加上编码转换格式后出问题了: The reference to entity"characterEncoding" must end with the ';' deli ...

  9. Maven 在 pom.xml 文件中配置 repositories 仓库

    如果你希望在你的项目中使用独立的 repositories . 例如,你希望配置使用自己的 https://maven.ossez.com/repository/internal 作为仓库. 例如,修 ...

随机推荐

  1. 理解AOP

    http://www.cnblogs.com/yanbincn/archive/2012/06/01/2530377.html Aspect Oriented Programming  面向切面编程. ...

  2. 自己随意写了个简单的依赖jquery的轮播图

    //轮播图 function Switcher(obj){ this.box = $(obj.box); this.width = this.box.width(); this.banner = $( ...

  3. Caffe(卷积神经网络框架)介绍

    Caffe(卷积神经网络框架)Caffe,全称Convolution Architecture For Feature Extraction caffe是一个清晰,可读性高,快速的深度学习框架.作者是 ...

  4. linux 搭建jenkins+创建maven工程JOB

    1.linux 安装jenkins 网上搜到这样安装 sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redh ...

  5. adb server is out of date killing... 的解决办法

    是adb server端口被占用了 你先执行adb nodaemon server ,查看adb server的端口是多少 1 2 C:\Users\xxxx>adb nodaemon serv ...

  6. oracle 游标使用大全

    转:http://www.cnblogs.com/fjfzhkb/archive/2007/09/12/891031.html oracle的游标和例子! 游标-----内存中的一块区域,存放的是se ...

  7. php pdo预处理语句与存储过程

    很多更成熟的数据库都支持预处理语句的概念.什么是预处理语句?可以把它看作是想要运行的 SQL 的一种编译过的模板,它可以使用变量参数进行定制.预处理语句可以带来两大好处: 1.查询仅需解析(或预处理) ...

  8. 编程语言java-并发(锁)

    文章转载自http://www.importnew.com/22078.html 悲观锁和乐观锁 我们都知道,CPU是时分复用的,就是CPU把时间片,分配给不同的thread/process轮流执行, ...

  9. Memcached监听多个端口_同一台Windows机器中启动多个Memcached服务

    下载Memcached服务器 假设你解压在"C:\Program Files\memcached\memcached.exe" 那么可以如下创建多个服务监听不同的端口啦 监听第一个 ...

  10. 无需部署的轻量级数据库—SQLLite,使用Demo

    当有程序需要保存轻量数据,而又烦躁序列化到本地的不便,轻量级数据库—SQLLite是一个很好的选择,只需引用System.Data.SQLite.DLL,无需部署数据库,便可像拥有数据库一样保存数据, ...