HibernateJavaStruts2

Struts 2, Hibernate 4 and MySQL Integration with Maven (Part 3)

Now we need to configure Hibernate and MySQL.

For this, lets add the dependencies for Hibernate jars and MySQL driver in the project’s pom.xml file.

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>5.1.34</version>

</dependency>

<dependency>

<groupId>org.hibernate</groupId>

<artifactId>hibernate-core</artifactId>

<version>4.3.10.Final</version>

</dependency>

Modify the versions according to the version you are using for MySQL and Hibernate.

After adding these dependencies, run following command under project root(path where pom.xml is present):

mvn validate

You should be seeing the corresponding Hibernate jars and MySQL driver jar referred in your project now.

Now, we need to add following for the hibernate changes.

Step 1:

Add hibernate configuration file hibernate.cfg.xml under src/main/resources folder.

Here are the contents of it :

<?xml version=“1.0”encoding=“UTF-8”?>

<!DOCTYPE hibernate-configuration PUBLIC “-//Hibernate/Hibernate Configuration DTD 3.0//EN”

“http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd”>

<hibernate-configuration>

<session-factory name=“”>

<property name=“hibernate.connection.driver_class”>com.mysql.jdbc.Driver</property>

<property name=“hibernate.connection.password”>password</property>

<property name=“hibernate.connection.url”>jdbc:mysql://localhost:3306/databasename</property>

<property name=“hibernate.connection.username”>username</property>

<property name=“hibernate.dialect”>org.hibernate.dialect.MySQLDialect</property>

<property name=“hibernate.show_sql”>true</property>

<property name=“hibernate.format_sql”>true</property>

<property name=“hibernate.hbm2ddl.auto”>create</property>

<mapping class=“com.techkatak.entity.Employee”/>

</session-factory>

</hibernate-configuration>

This file contains all the database specific configurations that Hibernate is going to need to interact with th database.

Please update username, password and connection url values according to your MySQL database settings.

The dialect and driver class are MySQL specific. If you are using a different database like Oracle or Sybase etc, these settings need to be updated too.

hbm2ddl property value of “create” tells hibernate to create the tables associated to the mapping class every time the application is started.

We have added a mapping for Employee class that we will create in next step.

Step 2:

Create a package com.techkatak.entity and Employee entity class in it.

Here is the code for Employee entity class.

package com.techkatak.entity;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;

@Entity

publicclass Employee {

@Id

@GeneratedValue(strategy=GenerationType.IDENTITY)

privateintid;

private String name;

privateintage;

publicint getId() {

returnid;

}

publicvoid setId(int id) {

this.id = id;

}

public String getName() {

returnname;

}

publicvoid setName(String name) {

this.name = name;

}

publicint getAge() {

returnage;

}

publicvoid setAge(int age) {

this.age = age;

}

}

The @Entity annotation identifies this as an entity class.

Step 3:

Create package com.techkatak.dao. We will create EmployeeDAO class and SessionUtil class in it.

The SessionUtil class would read the configuration file, create a SessionFactory and provide us a Session object that we will use in the DAO class to interact with the database.

Here is the code for it :

package com.techkatak.dao;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

publicclass SessionUtil {

privatestatic SessionUtil instance=new SessionUtil();

private SessionFactory sessionFactory;

publicstatic SessionUtil getInstance(){

returninstance;

}

private SessionUtil(){

Configuration configuration = new Configuration();

configuration.configure(“hibernate.cfg.xml”);

sessionFactory = configuration.buildSessionFactory();

}

publicstatic Session getSession(){

Session session = getInstance().sessionFactory.openSession();

return session;

}

}

 

In the EmployeeDAO.java, we will persist the Employee entity to the database.

package com.techkatak.dao;

import org.hibernate.Session;

import org.hibernate.Transaction;

import com.techkatak.bean.EmployeeBean;

import com.techkatak.entity.Employee;

publicclass EmployeeDAO {

publicvoid addEmployee(EmployeeBean bean){

Session session = SessionUtil.getSession();

Transaction tx = session.beginTransaction();

addEmployee(session,bean);

tx.commit();

session.close();

}

privatevoid addEmployee(Session session, EmployeeBean bean){

Employee employee = new Employee();

employee.setName(bean.getName());

employee.setAge(bean.getAge());

session.save(employee);

}

}

Here we are getting a transaction object from the session and using it to save the employee record in the database.

Finally, our application is ready .

Now, deploy the application on the server and access following url :

http://localhost:8080/Struts2HibernateApp/NewEmployee

You will get following screen :

registerAfter submitting the form, you should see following message on screen.

registersuccess

Here are the db interaction from the log :
log
If you query the database now, you should see the record you just added from the screen.
dbresult
This concludes the tutorial for integration of Struts 2, Hibernate 4, MySQL with Maven.

© 2015, www.techkatak.com. All rights reserved.

One thought on “Struts 2, Hibernate 4 and MySQL Integration with Maven (Part 3)

Comments are closed.