ZK MVVM CRUD With Spring 3 + JPA + Hibernate 4 Entity Manager–Part 3

Define the persistence.xml file

In the Part 2 article, we have seen how to create ZK Maven project and update the default POM.XML with all the necessary dependency required for this example.

Next, we need to configure the database to which we are going to connect to do our CRUD operation. So this file The persistence.xml file will contain database name, user name , password , etc.

In this post, we will see how to configure the database connection and how to configure the integration between hibernate and Spring.

What is persistence.xml file ?

The persistence.xml file is a standard configuration file in JPA. It has to be included in the META-INF directory inside the JAR file that contains the entity beans. The persistence.xml file must define a persistence-unit with a unique name in the current scoped classloader.

Now let us create our persistence.xml file.

Step 1:
In the Project navigator, under the folder “Main”, select new-> Folder and create a folder name “resource” as shown.
image
image

Now let us add this folder in the class path. In the Project navigator, select the addressbook project and right click and select “Properties”.

In the Properties window, Select “Java Build Path”  and in the source Tab, Click on Add folder
image

In the dialog, select the resource folder as shown.
image

After selecting, the source tab will look as follows
image

Next we will create another folder called META-INF under the resource folder as shown.
image

Step 2:
Now in the META-INT Folder, let me create the persistence.xml file
Select the META-INT Folder, and right click say New –> Other-> XML File
image

image

Here is the persistence.xml file content. You can copy and paste. But remember to change the databasename, username, password, etc according to your setup.

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
version="2.0">

<persistence-unit name="ZKExamples" >
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="1234" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.use_sql_comments" value="true" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/addressbook" />
</properties>
</persistence-unit>
</persistence>

The provider attribute specifies the underlying implementation of the JPA EntityManager. In this example, we will use Hibernate.
This is only place where we will say who is our JPA Vendor.


In next part 4, we will see how to integrate Spring and Hibernate.