In order to integrate the project with Spring and Hibernate a number of the files presented in Figure 5 have to be created. The content of the files is presented in the following part of this section.
database.properties
This file includes information used to connect to database. We use MySQL database to store our data. Make sure that jdbc.url, jdbc.username, and jdbc.password are updated with your settings.
log4j.properties
This file includes information used to log the data. For more information refer to log4j. Again, replace bold values with the correct settings.
DataSource.xml
Spring Bean defined to connect to database.
Hibernate.xml
Spring Bean defined to create session factory for hibernate.
applicationContext.xml
This file is used by Spring to create appropriate Spring Beans. For convenience it includes imports of appropriate files with Spring Beans. applicationContext.xml is default name and can be changed in web.xml file.
Updates to web.xml
In order to enable the integration the following changes should be done to web.xml file.
Entities
To communicate with database using Hibernate we have to create entities, which are layer between database and business interface of our application. The most convenient way to do that in Eclipse is to add MySQL database to data sources and create entity directly from the table. In order to do so, select:
File -> New -> Connection Profiles -> Connection Profile
Select: MySQL
Enter data as shown in Figure 6.
After clicking connect on the database in Data Source Explorer you should be able to see the following view (Figure 7). Note, that our database ‘integrationproject’ includes table ‘users’ which consists of 3 fields: ‘id’, ‘username’, and ‘password’.
Entity creation
To create entity from database table create JPA project:
File -> New -> JPA -> JPA Project
Enter:
Project name: IntegrationProjectJPA
Target runtime: [Tomcat server configured before]
Configuration: Utility JPA Project with Java 5.0
Following that select:
File -> New -> JPA -> Entities From Tables
Configure custom entities generation as shown in Figure 8.
Copy User.java file which was created to IntegrateProject under the path src/main/java/com/frogdroid/integration/user/entity/ and fix package name. Remove serialVersionUID variable. The content of User.java file is listed below.
XML mapping file creation
We will also need mapping file – it is required by Hibernate factory bean (Hibernate.xml). To create the mapping file select:
File -> New -> Hibernate -> Hibernate XML Mapping File (hbm.xml)
Select: Add Class… and enter: User
Select: User – com.frogdroid.integration.user.entity – IntegrationProject/src/main/java
The file User.hbm.xml will be generated. Copy it to src/main/resources/resources/hibernate/. Change ‘table=”USER”‘ to ‘table=”USERS”‘. If present remove serialVersionUID property. The content of the file is presented below.
Add mapping file to Hibernate configuration file (src/main/resources/resources/spring/config/Hibernate.xml) as follows:
Structure of the project after changes is presented in Figure 9.
Test that everything is working OK so far, run from the directory with pom.xml: