Hibernate Tutorial

Maven with Hibernate Example

Maven Hibernate example

This example gives you the list of dependency to add in maven for hibernate application

Maven dependency list for hibernate

Pom.xml

<dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
     <version>5.1.36</version>
   </dependency>
   <dependency>
     <groupId>org.hibernate</groupId>
     <artifactId>hibernate-core</artifactId>
     <version>5.0.2.Final</version>
   </dependency>
   <dependency>
     <groupId>antlr</groupId>
     <artifactId>antlr</artifactId>
     <version>2.7.7</version>
   </dependency>

Hibernate Hello world example with Maven

mysql.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC 
  "-//Hibernate/Hibernate Configuration DTD//EN"
  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory name="student1Factory">
          <property name="connection.driver_class">
               com.mysql.jdbc.Driver
          </property>
    <property name="connection.url">
      jdbc:mysql://localhost:3306/test
         
    </property>
    <property name="connection.username">
         root
    </property>
    <property name="connection.password">
        root
    </property>
          <property name="connection.pool_size">5</property>
          <!-- SQL dialect -->
          <property name="dialect">
               org.hibernate.dialect.MySQLDialect
          </property>
          <!-- Echo all executed SQL to stdout -->
          <property name="show_sql">true</property>
    <property name="hbm2ddl.auto">update</property>
    <mapping resource="User.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

 

User.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping >
  <class name="com.candidjava.hibernate.User" table="Usertbl5">
  <id name="id" type="long" column ="ID">
    <generator class="increment"/>
  </id>
  <property name="userName" column="UName" not-null="true"/>
  <property name="password" column="Pwd" not-null="true"/>
 
  </class>
</hibernate-mapping>

 

UserDao.java

public class UserDao {
  private static SessionFactory sessionFactory;
 
  private Session getSession() {
    Session s = null;
    // ljuouyhuko
    try {
      sessionFactory = new Configuration().configure("/mysql.cfg.xml")
          .buildSessionFactory();
      s = sessionFactory.openSession();
    } catch (HibernateException e) {
      System.out.println(e.getMessage());
    }
    return s;
  }
 
  public void insertUser(User u) {
    try {
      Session s = getSession();// getting sessionImpl reference
      Transaction tx = s.beginTransaction();
 
      s.save(u);
      tx.commit();
 
      System.out.println("\n\n username / password Added \n");
    } catch (HibernateException e) {
      System.out.println(e.getMessage());
    }
  }
 
}

Download

Hibernate Maven example