日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/4/17 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JPA 不在 persistence.xml 文件中配置每个Entity实体类的2种解决办法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

原文地址:http://www.cnblogs.com/taven/p/3351841.html

JPA 不在 persistence.xml 文件中配置每個Entity實體類的2種解決辦法

在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種方法都測試通過,還有沒有更簡單的辦法呢?你也可以將您的方式告訴遠哥,或在遠哥的博客下方留言,歡迎大家交流分享,謝謝。

轉載于:https://www.cnblogs.com/AloneSword/p/4558231.html

總結

以上是生活随笔為你收集整理的JPA 不在 persistence.xml 文件中配置每个Entity实体类的2种解决办法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。