In the tutorial Basic about JPA, I introduced with you all to the JPA configuration file for working with JPA, named persistence.xml. In this tutorial, I will explain more about declaration in this file.
As I said before, the persistence.xml file will contain information about one or more persistence units. Each persistence unit will include information about the entities that we will use, information about the database connection, username, password and much more. For a complete configuration file, we need to declare the name of the persistence unit, transaction-type, class, and properties.
Let’s review the persistence.xml file we created in the tutorial Basic about JPA:
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<persistence-unit name="jpaexample" transaction-type="RESOURCE_LOCAL">
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/hibernate_example" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="123456" />
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.use_sql_comments" value="true" />
You see, each persistence unit is declared in a tag named <persistence-unit>. If you want to add one more persistence unit, just add a new <persistence-unit> tag and add content to it.
The name attribute of the persistence-unit tag defines the name of the persistence-unit and will be used when you initialize the EntityManagerFactory. For example, in the tutorial Basic about JPA, we created the EntityManagerFactory by:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpaexample");
with jpaexample is the name of the persistence unit.
The second is the transaction-type attribute, which shows how our persistence unit will use the resource transaction. There are two values for transaction-type: RESOURCE_LOCAL and JTA. The meaning of each value is as follows:
- We will manage the creation and use of EntityManager.
- We must use EntityManagerFactory to get EntityManager.
- We can only use @PersistenceUnit annotation to get EntityManagerFactory.
- If you call EntityManagerFactory.createEntityManager() many times, it will give you more EntityManager, which should be avoided.
- The Web container will be responsible for managing the creation and use of EntityManager.
- You cannot use EntityManagerFactory to get an EntityManager that must be through a web container.
- We will use @PersistenceContext to get an EntityManager.
By default, if we do not declare transaction-type, then the value is JTA for the web environment and will be RESOURCE_LOCAL for the desktop application environment.
Usually, we should use the resource transaction RESOURCE_LOCAL to make it easier to create and use EntityManager.
The third thing we need to mention is the <class> tag, which declares the entities that will be used in our application. Many entities mean that we must declare many <class> tags. If I used entity Students in the previous tutorial, I would declare <class> tag as follows:
Declaring <exclude-unlisted-classes>true</ exclude-unlisted-classes> means that only classes declared in this configuration file are used as entities to manipulate the database.
The last thing we need to mention is the properties. As you can see, some properties are used to define the database connection information of our application.
We also need to know some other properties, as in our example:
- javax.persistence.schema-generation.database.action: This property specifies what the JPA will do with the database when our application is deployed. If we do not declare, then there will not be any operation that happens to our database. Its value is as follows:
- none: This value has the same meaning as when we did not declare this attribute. Nothing happens.
- create: JPA will create a new database when our application is deployed.
- drop-and-create: JPA will delete the old database and recreate the new database when our application is deployed.
- drop: Our database will be dropped when our application is deployed.
- hibernate.format_sql: properties are used in cases where you need to display the Hibernate SQL statement to the console and they must be formatted so that they can be easily read.
- hibernate.use_sql_comments: This property is used with Hibernate in case you want to know what the SQL statement is trying to do.
Some of the properties I will give more clearly with examples for you to understand in the other tutorial about JPA.