I'm writing a project using Maven and JPA (not a web app!).
I wrote my annotated entity classes and the CRUD service classes. But now, I am required to use javax.persistence.EntityManager
instead of org.hibernate.Session
to perform these CRUD operations.
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.
I know that in order to create EntityManagerFactory
like
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnitName)
,
then I have to have persistence.xml
under META-INF but I don't have this xml.
My question is: how do I migrate from Hibernate Sessions to JPA EntityManagers? And what's the equivalent of my hibernate config xml file in persistence config file?
Note: I cannot convert my project to JPA (there's no option).
Here's my 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
. 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. Folder must be declared in a registered source folder for your project, usually is 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
The persistence.xml is very similar, here:
<?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. 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
Or you can Help < Install New Software; http://download.eclipse.org/releases/luna or whatever version you have.
and then select JPA and EE plugins.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.