Entity Manager Factory

Any JPA-enabled application will require at least one EntityManagerFactory . Typically applications create one per datastore being utilised. An EntityManagerFactory provides access to EntityManager s which allow objects to be persisted, and retrieved. The EntityManagerFactory can be configured to provide particular behaviour.

The simplest way of creating an EntityManagerFactory in a J2SE environment is as follows

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;


EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnitName);

So you simply provide the name of the persistence-unit which defines the properties, classes, metadata etc to be used. An alternative is to specify the properties to use along with the persistence-unit name. In that case the passed properties will override any that are specified for the persistence unit itself.

Standard JPA Properties
Parameter Values Description
javax.persistence.provider Class name of the provider to use. DataNucleus has a provider name of org.datanucleus.jpa.PersistenceProviderImpl
javax.persistence.transactionType RESOURCE_LOCAL | JTA Type of transactions to use. In J2SE the default is RESOURCE_LOCAL. In J2EE the default is JTA.
javax.persistence.jtaDataSource JNDI name of a (transactional) JTA data source.
javax.persistence.nonJtaDataSource JNDI name of a (non-transactional) data source.
javax.persistence.jdbc.driver Alias for datanucleus.ConnectionDriverName
javax.persistence.jdbc.url Alias for datanucleus.ConnectionURL
javax.persistence.jdbc.user Alias for datanucleus.ConnectionUserName
javax.persistence.jdbc.password Alias for datanucleus.ConnectionPassword
javax.persistence.query.timeout Alias for datanucleus.query.timeout
javax.persistence.validation.mode AUTO | NONE | CALLBACK Determines whether the automatic lifecycle event validation is in effect
javax.persistence.validation.group.pre-persist A list of the targeted groups. A targeted group must be specified by its fully qualified class name. Names must be separated by a comma.
javax.persistence.validation.group.pre-update A list of the targeted groups. A targeted group must be specified by its fully qualified class name. Names must be separated by a comma.
javax.persistence.validation.group.pre-remove A list of the targeted groups. A targeted group must be specified by its fully qualified class name. Names must be separated by a comma.
javax.persistence.validation.factory A ValidatorFactory instance.

Extension DataNucleus Properties

DataNucleus provides many properties to extend the control that JPA gives you. These can be used alongside the above standard JPA properties, but will only work with DataNucleus. Please consult the Persistence Properties Guide for full details. In addition we have the following properties explicitly for JPA.

Description This property defines the level of JPA to be allowed. If you select JPA1 then you get strict JPA1, without DataNucleus extensions and without things like 1-N uni FK relations. If you select JPA2 then you get strict JPA2, without DataNucleus extensions. If you select "DataNucleus" you get full capabilities
Range of Values DataNucleus | JPA1 | JPA2

Description By default there is a ClassTransformer added to do enhancement at runtime with JPA. You can turn this off using this property. This is also useful to turn off the registration of the transformer so Spring doesn't throw exceptions
Range of Values true | false

Description JPA defines two lifecycle options. J2EE usage defaults to "transaction" where objects are detached when a transaction is committed. J2SE usage defaults to "extended" where objects are detached when the EntityManager is closed. This property allows control
Range of Values transaction | extended

Description JPA requires that any persistence exception should mark the current transaction for rollback. This persistence property allows that inflexible behaviour to be turned off leaving it to the user to decide when a transaction is needing to be rolled back.
Range of Values true | false