Hibernate tutorial: quickstart hibernate 4, mysql (annotation)

This tutorial show you how to make a java application and using hibernate to insert record into mysql database.

Tool and technologies using on this guide:

  • Eclipse kepler
  • Maven
  • Hibernate 4
  • Mysql 5.6

1. Create maven project

In Eclipse IDE,go to File->Project-> Maven Project

Choose simple Create a simple project and Next

Enter follow information

  • Group Id:com.devjav
  • Artifact Id:hibernate


2. Dependencies

We add follow dependencies to pom.xml file

    <!-- Hibernate -->
    <!-- Mysql -->
    <!-- test -->

3. Model class

We create model class User and annotation as below code

public class User implements Serializable {
  private static final long serialVersionUID = 7292222119906752946L;
  private Long id;
  private String username;
  private String password;
  private Date crtDate;

  @GeneratedValue(strategy = GenerationType.AUTO)
  public Long getId() {
    return id;

  public void setId(Long id) {
    this.id = id;

  public String getUsername() {
    return username;

  public void setUsername(String username) {
    this.username = username;

  public String getPassword() {
    return password;

  public void setPassword(String password) {
    this.password = password;

  @Column(name = "create_date")
  public Date getCrtDate() {
    return crtDate;

  public void setCrtDate(Date crtDate) {
    this.crtDate = crtDate;


4. Create hibernate configuration file

Create hibernate configuration file hibernate.cfg.xml in folder src/main/resources and put following content into that file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 <session-factory name="">
  <property name="hibernate.hbm2ddl.auto">update</property>
  <property name="hibernate.show_sql">true</property>
  <property name="hibernate.connection.password">admin</property>
  <property name="hibernate.connection.username">root</property>
  <property name="connection.url">jdbc:mysql://localhost/tutorial?autoReconnect=true</property>
  <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
  <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  <mapping class="com.devjav.tutorial.hibernate.entity.User"/>

*Note: you should install Hibernate Tool for save time when develop Hibernate application. Please check this link to know how to install that

5. Create hibernate Util class

Create HibernateUtil.java to manage Hibernate startup and retrieve Hibernate session

public class HibernateUtil {
  private static SessionFactory sf;

  private synchronized static void init() {
    if (sf == null) {
      try {
        Configuration cfg = new Configuration().configure();
        StandardServiceRegistryBuilder serviceRegistry = new StandardServiceRegistryBuilder()
        sf = cfg.buildSessionFactory(serviceRegistry.build());
      } catch (Exception ex) {

  public static Session openSession() {
    return sf.openSession();


6. Application Entry

We create App class to do simple task insert record to database.

public class App {

   * @param args
  public static void main(String[] args) {
    Session session = null;
    Transaction txn = null;
    try {
      session = HibernateUtil.openSession();
      User user = new User();
      user.setCrtDate(new Date());
      txn = session.beginTransaction();
    } catch (Exception ex) {
      if (txn != null) {
    } finally {
      if (session != null) {



7. Run application

Run this application and it will insert a record users table

Hibernate: insert into users (create_date, password, username) values (?, ?, ?)

8. Source code

You can download source code by click this link

One thought on “Hibernate tutorial: quickstart hibernate 4, mysql (annotation)”

Leave a Reply