In the previous post I created the hibernate mapping files for the Person-Address domain classes. In here we are going to create the hibernate configuration file which creates the database connection according to the defined URL, credentials and other properties.
On the deplyment of the project, the hibernate configuration file is executed through the commands in pom.xml.
We have to put the hibernate configuration file inside the src/main/resources directory. For that right cick on the directory, select new, select other. From there expand the hibernate icon and select Hibernate Configuration file (cfg.xml).
Keep the default name "hibernate.cfg.xml" same and click Next
Give the values in the appeared window give the values as follows.
Make sure to verify your mysql username and password, and enter the corret one according to your mysql configuration.
Add the mapping resource entries to your hibernate.cfg.xml file as above.
com.mysql.jdbc.Driver root jdbc:mysql://localhost:3306/testdb1 root org.hibernate.dialect.MySQLDialect
Here is the directory structureof the project after the hibernate.config.xml file is being added.
Let's add the build elements to the pom.xml file to execute the mapping files and create the tables accordingly.
append the following etries to the pom.xml file inside the project tags.
org.apache.maven.plugins maven-compiler-plugin 1.5 1.5 maven-resources-plugin org.apache.maven.plugins maven-eclipse-plugin 2.7 org.codehaus.mojo hibernate3-maven-plugin org.apache.maven.plugins maven-source-plugin target false make-source-jar package jar org.codehaus.mojo hibernate3-maven-plugin 2.2 process-classes hbm2ddl hsqldb hsqldb 18.104.22.168 mysql mysql-connector-java 5.1.6 hbm2ddl target/classes/hibernate.cfg.xml schema.sql true true true false maven-checkstyle-plugin
It is ready to execute the project and one more thing left. Go to mysql command line and create a database called testdb1.
Deploying the project
Right click on the project and goto run as--maven install
In the maven console you can examine the execution. It will display the sql of the tables created. If you have followed the things correctly at the end it will display BUILD SUCCESSFUL.
Inside the target directory hibernate/sql will contain the scema.sql file which is a dump file of the created tables. Also the jar files are created. (testapp-0.0.1-SNAPSHOT.jar and tetsapp-0.0.1-SNAPSHOT-source.jar is created)
The tables can be viewed through mysql command line.
Comments and Questions are wel come