在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. 对CLR基本原理概念&垃圾回收机制的简单理解

    前言,之前有说过C语言的函数&变量的一些基本概念,说得可能不是很好,先也把C#的.里相关的也说下,已成一统. 而说函数变量,其实主要就是GC,而GC又是CLR的主要内容,故就有了此文. CLR ...

  2. python学习笔记-Day6(3)

    代码书写原则: 1)不能重复写代码 2)写的代码要经常变更 编程模式概述 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数 ...

  3. Eclipse 3.5使用dropins的插件安装方式

    以前安装Eclipse插件有两种方式 1 直接copy插件到features/plugins目录 2 在links目录下创建链接文件. 而 Eclipse 3.5又推出另一种新的安装途径, 更加灵活. ...

  4. 0729pm命名空间

  5. 在UTF8(linux)下,逆置汉字字符串

    #include <stdio.h> int main() { char c[]="我是如此热爱编程!"; ,min=,max; while(c[index]) { i ...

  6. homework 15 2016 6 2 模板

    #include<iostream>#include<cmath>#include<cstdio> using namespace std; template &l ...

  7. .net 泛型运用

    DAL层 private DbContext MyContext; public BaseRepository(DbContext context) { MyContext = context; } ...

  8. 在CentOS6.7操作系统上编译安装httpd2.4

    功能描述: 在CentOS6.7操作系统上,编译安装apache服务,实现定制功能等 一.安装前提 1)安装编译httpd需要的软件包 [root@bqe6tewv41kx ~]#  yum -y i ...

  9. 关于e^PI>PI^e

  10. 什么是领域驱动设计(Domain Driven Design)?

    本文是从 What is Domain Driven Design? 这篇文章翻译而来. ”…在很多领域,专家的作用体现在他们的专业知识上而不是智力上.“ -- Don Reinertsen 领域驱动 ...