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.
Figure 5: Files needed for integration with Hibernate and Spring.
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.
This file includes information used to log the data. For more information refer to log4j. Again, replace bold values with the correct settings.
Spring Bean defined to connect to database.
Spring Bean defined to create session factory for hibernate.
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.
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
Enter data as shown in Figure 6.
Figure 6: Configuration of MySQL connection in Eclipse.
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’.
Figure 7: View on database in Eclipse.
To create entity from database table create JPA project:
File -> New -> JPA -> JPA Project
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.
Figure 8: Generation of custom entities.
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.
Figure 9: Project with entity.
Test that everything is working OK so far, run from the directory with pom.xml: