0

I have a servlet, and when I call this method with H2 specified in the PU, it re-creates all the database structure each time I call it. Can I only call this method 1 time, and if I can call it > 1 time, how do I do it?

entityManagerFactory = Persistence
                    .createEntityManagerFactory("MYPU");

XML for persistence

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
 http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="MyJPAJAXRS" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <non-jta-data-source>jdbc/myds</non-jta-data-source>
    <properties>
      <property name="javax.persistence.schema-generation.database.action"
                value="drop-and-create"/>
      <property name="javax.persistence.sql-load-script-source" value="META-INF/seed.sql"/>
    </properties>
  </persistence-unit>
</persistence>
Gray
  • 115,027
  • 24
  • 293
  • 354
mikeb
  • 10,578
  • 7
  • 62
  • 120

2 Answers2

3

You'll need a singleton method to create EntityManagerFactory and then get a new instance of EntityManager using that Singleton Instance. Note: EntityManager is not thread safe and you'll need to get a new EntityManager instance per thread. Below is an example of how to implement this

public class JpaUtil {

private static HashMap<String, String> properties = new HashMap<String, String>();
private volatile static EntityManagerFactory factory;

static {
    properties.put("javax.persistence.jdbc.driver", System.getProperty("DRIVER"));
    properties.put("javax.persistence.jdbc.user", System.getProperty("USER"));
    properties.put("javax.persistence.jdbc.password", System.getProperty("PASSWORD"));
    properties.put("javax.persistence.jdbc.url", System.getProperty("DATABASEURL"));
}

private static EntityManagerFactory getInstance() {
    if (factory == null) {
        synchronized (EntityManagerFactory.class) {
            if (factory == null) {

                factory = Persistence.createEntityManagerFactory("PU", properties);
            }
        }
    }
    return factory;
}

public static EntityManager getEntityManager() throws Exception {
    return getInstance().createEntityManager();
}

}

And then to get the Entity manager simply call: JpaUtil.getEntityManager()

Shivam Sinha
  • 4,924
  • 7
  • 43
  • 65
  • Thanks, one qq though - why the properties? I have the connection setup as a JNDI resource that I reference from persistence.xml. I can skip that, yes? – mikeb Mar 10 '17 at 23:24
  • That was just for an example. If you are already setting the props in your persistence.xml then there is no need to set the properties here. Just use the other overloaded method: createEntityManagerFactory(String persistenceUnitName) – Shivam Sinha Mar 10 '17 at 23:27
  • Also please accept if I have answered your original question. Thanks. – Shivam Sinha Mar 10 '17 at 23:28
1

You should have a singleton entityManagerFactory , then you can call createEntityManager how many time you want.

minus
  • 2,646
  • 15
  • 18