About Persistence Units

See Also

A persistence unit is required if you are using Java Persistence in your application. A persistence unit is a uniquely-named collection of properties that are used to determine how a specific set of entities in an application are managed and persisted.

Persistence units are defined in the persistence.xml file. You can have more than one persistence unit defined in persistence.xml, but each persistence unit must have a unique name.

Properties specified in a persistence unit include the following:

Persistence units can be packaged as part of a WAR or EJB-JAR file, or can be packaged as a JAR file that can then be included in your application.

Scope of the Persistence Unit

The scope of a persistence unit is determined by the location of persistence.xml. When you use a wizard to create a persistence unit, the IDE creates persistence.xml in the location appropriate for the scope. For example, if you use the IDE to create a persistence unit for an EJB module, the IDE generates persistence.xml in the src/conf directory of your EJB module. When the EJB JAR is built, it packages persistence.xml in the EJB JAR's META-INF directory. The scope of the persistence unit is the set of classes in the EJB JAR file.

If the scope is not explicitly specified in a persistence unit, by default all the entities in the EJB JAR file would be within the scope of the persistence unit defined in persistence.xml.

Notes:

Persistence Provider

A persistence provider refers to an implementation of the Java Persistence API. The persistence provider is a library that provides the functionality to persist objects in the application.

The IDE is bundled with the EclipseLink persistence provider. EclipseLink is the reference implementation and the default Java Persistence provider in the GlassFish v3 application server. You can use EclipseLink as your provider, or specify a different persistence provider.

Data Source

A data source refers to the database where persistent entities are stored. The data source must be registered on the server and is specified using the JNDI name. If the transactions are container-managed JTA transactions, the data source must be a JTA data source. If the transactions are application-managed, the data source is specified according to the JDBC database connection registered with the IDE.

In Java SE environments, the database is specified either using a data source or by other means, depending on the requirements of the persistence provider used.

Transaction Types

A persistence unit specifies how transactions are managed in the application. The transaction type you can use depends on the target container. If you are deploying to a Java EE container, transactions can be container-managed or application-managed. If you are not deploying to a Java EE container, transactions must be managed by the application.

See Also
Working with Enterprise Applications
Creating a Persistence Unit
About Java Persistence
About Entity Managers

Legal Notices