Oct 26, 2018

Java JPA persistence unit and persistence.xml

The list of entities in your application is called a persistence unit, and the application's persistence unit is defined in the persistence.xml configuration file. This file should reside in your application's META-INF directory. The significant elements of persistence.xmlfollow:

  • Persistence unit name—You can give the persistence unit a name so that you can define several persistence units and then select them at run time.
  • Persistence unit transaction type—In a Java SE application, the default transaction type is RESOURCE_LOCAL, while in a Java EE environment, the transaction type is JTA. This means that the entity manager participates in the transaction.
  • Provider—This element identifies the class that provides the factory for creating the EntityManagerinstance.
  • Class—The entity classes used in the application should be listed in the class element.
  • Property—Additional properties can be specified, such as database connection properties and persistence provider properties like options to drop-create new tables.