Search code examples
openjpa

How to create an EntityManager object?


I am new to Openjpa. I need to explicitly create an EntityManager object, for a persistence unit named Draco-PU. I have the following persistence xml defined. Please help!

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
<persistence-unit name="Draco-PU" >
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<jta-data-source>DracoDataSource</jta-data-source>
<non-jta-data-source>DracoUnmanagedDataSource</non-jta-data-source>
<class>dk.tdc.soa.smo.draco.db.model.DslServiceEntity</class>
<class>dk.tdc.soa.smo.draco.db.model.DslServiceCatalogEntity</class>
<class>dk.tdc.soa.smo.draco.db.model.History</class>
<class>dk.tdc.soa.smo.draco.db.model.ConfigEntity</class>
<properties>
  <property name="openjpa.Log" value="DefaultLevel=WARNING, Runtime=WARNING, Tool=WARNING, SQL=WARNING" />
  <!-- <property name="openjpa.jdbc.DBDictionary" value="org.apache.openjpa.jdbc.sql.OracleDictionary" />  -->     
  <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)" />
</properties>
</persistence-unit>
</persistence>

Solution

  • I was new two weeks ago, so my answer is worth what it's worth but I do like that:

    EntityManager entityManager = 
    Persistence.createEntityManagerFactory("nameofyourpersistenceunit").createEntityManager();