Spring-Hibernate Maven project- Part 3

Hibernate Configuration and Project Deployment


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).

Click Next

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.

hibernate.cfg.xml
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">
root</property>
        <property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/testdb1</property>
        <property name="hibernate.connection.username">
root</property>
        <property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect</property>
        
        <mapping resource="hibernate/Person.hbm.xml">
        <mapping resource="hibernate/Address.hbm.xml">
    </mapping>
</mapping>
</session-factory></hibernate-configuration>
Add the mapping resource entries to your hibernate.cfg.xml file as above.
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.
 <build>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
     <source>1.5</source>
     <target>1.5</target>
    </configuration>
   </plugin>
   <plugin>
    <artifactId>maven-resources-plugin</artifactId>
   </plugin>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-eclipse-plugin</artifactId>
    <version>2.7</version>
   </plugin>
   <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>hibernate3-maven-plugin</artifactId>
   </plugin>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-source-plugin</artifactId>
    <version>3.0.1</version>
    <configuration>
     <outputDirectory>target</outputDirectory>
     <finalName>
      <attach>false</attach>
     </finalName>
     <executions>
      <execution>
       <id>make-source-jar</id>
       <phase>package</phase>
       <goals>
        <goal>jar</goal>
       </goals>
      </execution>
     </executions>
    </configuration>
    <plugin>
     <groupId>org.codehaus.mojo</groupId>
     <artifactId>hibernate3-maven-plugin</artifactId>
     <version>2.2</version>
     <executions>
      <execution>
       <phase>process-classes</phase>
       <goals>
        <goal>hbm2ddl</goal>
       </goals>
      </execution>
     </executions>
     <dependencies>
      <dependency>
       <groupId>hsqldb</groupId>
       <artifactId>hsqldb</artifactId>
       <version>1.8.0.7</version>
      </dependency>
      <dependency>
       <groupId>mysql</groupId>
       <artifactId>mysql-connector-java</artifactId>
       <version>5.1.6</version>
      </dependency>
     </dependencies>
     <configuration>
      <component>
       <name>hbm2ddl</name>
      </component>
      <componentproperties>
       <configurationfile>target/classes/hibernate.cfg.xml
       </configurationfile>
       <outputfilename>schema.sql</outputfilename>
       <export>true</export>
       <format>true</format>
       <drop>true</drop>
       <haltonerror>false</haltonerror>
      </componentproperties>
     </configuration>
    </plugin>
    <plugin>
     <artifactId>maven-checkstyle-plugin</artifactId>
    </plugin>
   </plugin>
  </plugins>
 </build>

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
Post a Comment

Popular posts from this blog

Yield Price Sri Lanka - Android Application

Manage SOAPAction of the Out Message

How to clean Registry log (REG_LOG) table