Introducing the persistence.xml file
The persistence.xml
file is generated during the reverse engineering process and defines the JPA configuration for a set of entity classes. This file is always located in the META-INF
directory at the root of the classpath. Maven projects have a special directory named resources
located in the src/main
directory, which contains additional resources applicable for building the Java project. The resources
directory is automatically copied by Maven to the root of the classpath when building the project. Open the file by double-clicking on it to display the Design view of the file in the editor:
The Design view contains several properties that are used to configure the persistence unit behavior. We will stick to the simplest settings, but we encourage you to explore the different strategies that may be useful for your own application's needs. For example, projects that require tables to be automatically created will appreciate the Table Generation Strategy of Create or Drop and Create. Selecting the different options and switching to the Source view will help us to quickly identify the appropriate properties in the persistence.xml
file.
Click on the Source button at the top to view the default file contents in the text format:
Change the default persistence-unit
node name
attribute value to tttPU
instead of the long autogenerated name. This value will be used in your Java code to refer to this persistence unit and is easy to remember. The provider
node value is automatically set to the appropriate EclipseLink class, and the jta-data-source
node value is automatically set to the data source used during the reverse engineering wizard. The exclude-unlisted-classes
setting will define whether the classpath is scanned for annotated entity classes. Change this to false
. For large projects, this is the safest way of ensuring that classes are not omitted accidentally. It is also possible to specify each class explicitly in the following way:
This is fine for small projects but not very practical if you have hundreds of entity classes. In the previous example, the exclude-unlisted-classes
property is set to true
, meaning that only the specified classes will be loaded without the need for classpath scanning. We prefer the first method for defining our JPA classes, where the classpath is scanned for all the annotated entity classes by setting exclude-unlisted-classes
to false
.
The final configuration item of interest is the transaction-type
attribute. There are two different types of transactions supported by this item, of which we have JTA
set by default. JTA (Java Transaction API) denotes that transactions will be managed by a Java EE transaction manager provided by the GlassFish server in our application. We will explore the RESOURCE_LOCAL
alternative to JTA when we build our test cases in Chapter 5, Testing the DAO Layer with Spring and JUnit. In this situation, the transactions will be managed locally without a Java EE container.