jparuntimeentitymanagerpersistence.xml

Create JPA EntityManager without persistence.xml configuration file


Is there a way to initialize the EntityManager without a persistence unit defined? Can you give all the required properties to create an entity manager? I need to create the EntityManager from the user's specified values at runtime. Updating the persistence.xml and recompiling is not an option.

Any idea on how to do this is more than welcomed!


Solution

  • Is there a way to initialize the EntityManager without a persistence unit defined?

    You should define at least one persistence unit in the persistence.xml deployment descriptor.

    Can you give all the required properties to create an Entitymanager?

    <persistence>
        <persistence-unit name="[REQUIRED_PERSISTENCE_UNIT_NAME_GOES_HERE]">
            SOME_PROPERTIES
        </persistence-unit>
    </persistence>
    

    In Java EE environments, the jta-data-source and non-jta-data-source elements are used to specify the global JNDI name of the JTA and/or non-JTA data source to be used by the persistence provider.

    So if your target Application Server supports JTA (JBoss, Websphere, GlassFish), your persistence.xml looks like:

    <persistence>
        <persistence-unit name="[REQUIRED_PERSISTENCE_UNIT_NAME_GOES_HERE]">
            <!--GLOBAL_JNDI_GOES_HERE-->
            <jta-data-source>jdbc/myDS</jta-data-source>
        </persistence-unit>
    </persistence>
    

    If your target Application Server does not support JTA (Tomcat), your persistence.xml looks like:

    <persistence>
        <persistence-unit name="[REQUIRED_PERSISTENCE_UNIT_NAME_GOES_HERE]">
            <!--GLOBAL_JNDI_GOES_HERE-->
            <non-jta-data-source>jdbc/myDS</non-jta-data-source>
        </persistence-unit>
    </persistence>
    

    If your data source is not bound to a global JNDI (for instance, outside a Java EE container), so you would usually define JPA provider, driver, url, user and password properties. But property name depends on the JPA provider. So, for Hibernate as JPA provider, your persistence.xml file will looks like:

    <persistence>
        <persistence-unit name="[REQUIRED_PERSISTENCE_UNIT_NAME_GOES_HERE]">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <class>br.com.persistence.SomeClass</class>
            <properties>
                <property name="hibernate.connection.driver_class" value="org.apache.derby.jdbc.ClientDriver"/>
                <property name="hibernate.connection.url" value="jdbc:derby://localhost:1527/EmpServDB;create=true"/>
                <property name="hibernate.connection.username" value="APP"/>
                <property name="hibernate.connection.password" value="APP"/>
            </properties>
        </persistence-unit>
    </persistence>
    

    Transaction Type Attribute

    In general, in Java EE environments, a transaction-type of RESOURCE_LOCAL assumes that a non-JTA datasource will be provided. In a Java EE environment, if this element is not specified, the default is JTA. In a Java SE environment, if this element is not specified, a default of RESOURCE_LOCAL may be assumed.

    I need to create the EntityManager from the user's specified values at runtime

    So use this:

    Map addedOrOverridenProperties = new HashMap();
    
    // Let's suppose we are using Hibernate as JPA provider
    addedOrOverridenProperties.put("hibernate.show_sql", true);
    
    Persistence.createEntityManagerFactory(<PERSISTENCE_UNIT_NAME_GOES_HERE>, addedOrOverridenProperties);