[英]Migrate from Hibernate to Persistence
我正在使用Maven和JPA(不是Web應用程序!)編寫項目。
我編寫了帶注釋的實體類和CRUD服務類。 但是現在,我需要使用javax.persistence.EntityManager
而不是org.hibernate.Session
來執行這些CRUD操作。
我使用Hibernate作為JPA提供程序,並且在hibernate.cfg.xml
resources文件夾下配置了所有內容,並且目前一切正常。
我知道為了創建像這樣的EntityManagerFactory
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnitName)
,
那么我必須在META-INF下有persistence.xml
,但是我沒有這個xml。
我的問題是:如何從休眠會話遷移到JPA EntityManager? 在持久性配置文件中相當於我的休眠配置xml文件又是什么呢?
注意:我無法將項目轉換為JPA(沒有選擇)。
這是我的hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property>
<property name="hibernate.connection.driver_class">org.apache.derby.jdbc.EmbeddedDriver</property>
<property name="hibernate.connection.url">jdbc:derby:D:/db/derby/svn;create=true</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.current_session_context_class">org.hibernate.context.internal.ThreadLocalSessionContext</property> <!-- org.hibernate.context.internal.ManagedSessionContextThreadLocalSessionContext -->
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<mapping class="entity.Pe" />
<mapping class="entity.Us" />
<mapping class="entity.Te" />
</session-factory>
</hibernate-configuration>
persistence.xml
與hibernate.cfg.xml
非常相似。 在此之后,您可以找到一個非常簡單的比較。
我認為“ 只要創建一個文件夾META-INF並在其下放置等效的persistence.xml ”就可以解決問題。 必須在項目的已注冊源文件夾中聲明文件夾,通常是src/main/resources/META-INF
。
然后,您可以執行以下行:
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("YourPersistenceUnitNameHere")
您只需在src/main/java/
下創建包含persistence.xml
META-INF
文件夾,即可將其轉換為JPA項目。
persistence.xml非常相似,在這里:
<?xml version="1.0" encoding="UTF-8"?>
<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="persistence-unit-name">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>entity.Pe</class>
<class>entity.Us</class>
<class>entity.Te</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect" />
<property name="hibernate.connection.driver_class" value="org.apache.derby.jdbc.EmbeddedDriver" />
<property name="hibernate.connection.url" value="jdbc:derby:D:/db/derby/svn;create=true" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="root" />
<property name="hibernate.current_session_context_class"
value="org.hibernate.context.internal.ThreadLocalSessionContext" />
<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>
沒有選項project-RightClick <配置<轉換為JPA的原因是因為您的Eclipse版本不是Enterprise Edition。 對?
下載最新版本的Eclipse IDE:Eclipse Mars2 J2EE: http ://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/mars/2/eclipse-jee-mars-2 -win32-x86_64.zip
或者您可以幫助<安裝新軟件; http://download.eclipse.org/releases/luna或您使用的任何版本。
然后選擇JPA和EE插件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.