Is there a way to initialize an EntityManager without a specific save unit?
In the deployment descriptor persistence.xml you must define at least one persistence block.
Can you provide all the necessary properties to create an EntityManager ?
- The name attribute is required. Other attributes and elements are optional. (JPA specification). Thus, it should be a more or less minimal
persistence.xml file:
<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 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 this:
<persistence> <persistence-unit name="[REQUIRED_PERSISTENCE_UNIT_NAME_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 this:
<persistence> <persistence-unit name="[REQUIRED_PERSISTENCE_UNIT_NAME_GOES_HERE]"> <non-jta-data-source>jdbc/myDS</non-jta-data-source> </persistence-unit> </persistence>
If your data source is not tied to a global JNDI (for example, outside a Java EE container), you usually define the properties of the provider, driver, URL, user and password of the JPA. But the name of the property depends on the JPA provider. So, for Hibernate as a JPA provider, your persistence.xml file will look like this:
<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, the transaction type RESOURCE_LOCAL assumes that a non-JTA data source will be provided. In Java EE, if this item is not specified, JTA is used by default. In Java SE, if this item is not specified, the default value RESOURCE_LOCAL may be accepted.
- To ensure portability of a Java SE application , you must explicitly specify the managed persistence classes that are included in the persistence block (JPA specification)
I need to create an EntityManager from user defined values ββat runtime
So use this:
Map addedOrOverridenProperties = new HashMap();