Easy learning with example program codes

Eclipse maven hibernate project

Eclipse maven hibernate project:

Eclipse provides m2eclipse plugin to integrate Maven and Eclipse together.

Steps to create maven java project in eclipse:

  1. In eclipse, click on File menu → New → Maven Project. Select maven-archetype-quickstart template to create java project and Click on Next button.
  2. Now provide the group Id, artifact Id and Package. Click on Finish button. Complete directory structure and all files like Java file, pom.xml file, test case file etc will be created automatically.

Directory structure of the maven java project:

Add hibernate dependencies to automatically generated pom.xml file.

<project xmlns="" 
    <!-- JUnit 3.8.1 -->
    <!-- Hibernate 4.3.6 Final -->
    <!-- Mysql Connector -->

Create hibernate.cfg.xml file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test_schema</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">pannu#519699</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
        <mapping class="com.tutorialspointexamples.Users"></mapping>

Create an entity class

package com.codesjava;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Table(name= "users") 
public class Users {
	private int seq;
	private String userName;
	private String password;
	private Date createDate;
	private String createUser;
	private Date modifyDate;
	private String modifyUser;
	public Users(){
	@Column(name = "SEQ", unique = true, nullable = false)
	public int getSeq() {
		return seq;
	public void setSeq(int seq) {
		this.seq = seq;
	@Column(name = "USER_NAME")
	public String getUserName() {
		return userName;
	public void setUserName(String userName) {
		this.userName = userName;
	@Column(name = "PASSWORD")
	public String getPassword() {
		return password;
	public void setPassword(String password) {
		this.password = password;
	@Column(name = "CREATE_DATE")
	public Date getCreateDate() {
		return createDate;
	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	@Column(name = "CREATE_USER")
	public String getCreateUser() {
		return createUser;
	public void setCreateUser(String createUser) {
		this.createUser = createUser;
	@Column(name = "MODIFY_DATE")
	public Date getModifyDate() {
		return modifyDate;
	public void setModifyDate(Date modifyDate) {
		this.modifyDate = modifyDate;
	@Column(name = "MODIFY_USER")
	public String getModifyUser() {
		return modifyUser;
	public void setModifyUser(String modifyUser) {
		this.modifyUser = modifyUser;

Create Hibernate Utility class

package com.codesjava;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
	private static final SessionFactory sessionFactory = buildSessionFactory();
	private static SessionFactory buildSessionFactory() {
	 SessionFactory sessionFactory = null;
	 try {
	   //Create the session factory object.
	   return new  Configuration().configure().buildSessionFactory(); 
	 catch (Exception e) {
	 return sessionFactory;
	public static SessionFactory getSessionFactory() {
	  return sessionFactory;


package com.codesjava;
import java.util.Date;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.codesjava.HibernateUtil;
public class App 
    public static void main( String[] args ) {
    	//Create the student object.
    	Users user = new Users();
    	//Setting the object properties.
    	user.setCreateDate(new Date());
    	Transaction tx = null;
    	//Get the session object.
    	Session session = HibernateUtil.getSessionFactory().openSession();
              //Start hibernate session.
    	      tx = session.beginTransaction();
              //Insert a new student record in the database.
              //Commit hibernate transaction if no exception occurs.
    	      System.out.println("Saved Successfully.");
    	  }catch (HibernateException e) {
                 //Roll back if any exception occurs. 
    	  }finally {
             //Close hibernate session.


insert into users (CREATE_DATE, CREATE_USER, MODIFY_DATE, MODIFY_USER, PASSWORD, USER_NAME, SEQ) values (?, ?, ?, ?, ?, ?, ?)
Saved Successfully.

Copyright © 2018 CodesJava Protection Status SiteMap