[英]Migrate from Hibernate to Persistence
I'm writing a project using Maven and JPA (not a web app!). 我正在使用Maven和JPA(不是Web应用程序!)编写项目。
I wrote my annotated entity classes and the CRUD service classes. 我编写了带注释的实体类和CRUD服务类。 But now, I am required to use javax.persistence.EntityManager
instead of org.hibernate.Session
to perform these CRUD operations. 但是现在,我需要使用javax.persistence.EntityManager
而不是org.hibernate.Session
来执行这些CRUD操作。
I'm using Hibernate as JPA provider and I have everything configured on hibernate.cfg.xml
under resources folder and things are running properly for now. 我使用Hibernate作为JPA提供程序,并且在hibernate.cfg.xml
resources文件夹下配置了所有内容,并且目前一切正常。
I know that in order to create EntityManagerFactory
like 我知道为了创建像这样的EntityManagerFactory
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnitName)
, EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnitName)
,
then I have to have persistence.xml
under META-INF but I don't have this xml. 那么我必须在META-INF下有persistence.xml
,但是我没有这个xml。
My question is: how do I migrate from Hibernate Sessions to JPA EntityManagers? 我的问题是:如何从休眠会话迁移到JPA EntityManager? And what's the equivalent of my hibernate config xml file in persistence config file? 在持久性配置文件中相当于我的休眠配置xml文件又是什么呢?
Note: I cannot convert my project to JPA (there's no option). 注意:我无法将项目转换为JPA(没有选择)。
Here's my hibernate.cfg.xml 这是我的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
is very similar to hibernate.cfg.xml
. persistence.xml
与hibernate.cfg.xml
非常相似。 Following this you can find a very straightforward comparative. 在此之后,您可以找到一个非常简单的比较。
I think that " just create a folder META-INF and place under it the equivalent persistence.xml " should do the trick. 我认为“ 只要创建一个文件夹META-INF并在其下放置等效的persistence.xml ”就可以解决问题。 Folder must be declared in a registered source folder for your project, usually is src/main/resources/META-INF
. 必须在项目的已注册源文件夹中声明文件夹,通常是src/main/resources/META-INF
。
Then you can execute this line: 然后,您可以执行以下行:
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("YourPersistenceUnitNameHere")
You can simply convert it to JPA project by creating META-INF
folder under src/main/java/
containing persistence.xml
您只需在src/main/java/
下创建包含persistence.xml
META-INF
文件夹,即可将其转换为JPA项目。
The persistence.xml is very similar, here: 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>
The reason there's no option project-RightClick < Configure < Convert to JPA is because your version of Eclipse is not Enterprise Edition. 没有选项project-RightClick <配置<转换为JPA的原因是因为您的Eclipse版本不是Enterprise Edition。 Right? 对?
Download the latest version of 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 下载最新版本的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
Or you can Help < Install New Software; 或者您可以帮助<安装新软件; http://download.eclipse.org/releases/luna or whatever version you have. http://download.eclipse.org/releases/luna或您使用的任何版本。
and then select JPA and EE plugins. 然后选择JPA和EE插件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.