繁体   English   中英

从休眠模式迁移到持久性

[英]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.xmlhibernate.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM