Showing posts with label session. Show all posts
Showing posts with label session. Show all posts

Thursday, June 5, 2014

hibernate: get vs load

The difference between get() and load() methods of class Session is


get(Student.class, 234) -  returns the entity instance of the class(Student) from  table student matched with the unique identifier value 234.

load(Student.class, 234) - returns the entity instance of the class(Student) from  table student matched with the unique identifier value 234.

But the exact difference between them is get returns null when there is no match in the DB table while,  the load method throws exception "org.hibernate.ObjectNotFoundException: No row with the given identifier exists".

Wednesday, June 4, 2014

Hibernate sample code save()



This is the Hibernate sample code which inserts a record set into a table.
Create a Java project HibernateCRUDSampel in Eclipse project explorer.

1.    Create a folder structure.



2.    Create hibernate.cfg.xml. 

<?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>
            <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
            <property name="connection.url">jdbc:mysql://localhost/testapp</property>
            <property name="connection.username">root</property>
            <property name="connection.password">password</property>
            <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="connection.show_sql">true</property>
            <mapping resource="resource/Student.hbm.xml" />
      </session-factory>
</hibernate-configuration>
 

3.    Create a Student mapping file.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
      <class name="sample.hiber.crud.model.Student" table="student">
            <id name="studentId" type="java.lang.Integer">
                  <column name="student_id" />
                  <generator class="identity" />
            </id>
            <property name="regNo" type="java.lang.Long">
                  <column name="reg_no" />
            </property>
            <property name="firstName" type="string">
                  <column name="first_name" />
            </property>
            <property name="lastName" type="string">
                  <column name="last_name" />
            </property>
            <property name="age" type="java.lang.Integer">
                  <column name="age" />
            </property>
            <property name="sex" type="java.lang.Character">
                  <column name="sex" />
            </property>
            <property name="mailId" type="string">
                  <column name="mail_id" />
            </property>
            <property name="mobileNo" type="string">
                  <column name="mobile_no" />
            </property>
      </class>
</hibernate-mapping>
 

 

4.    Create a table student in MySQL with following structure





5.    Create a mapping object Class Student.java


public class Student implements Serializable{

      private static final long serialVersionUID = -1097973765295431159L;
      private int studentId;
      private long regNo;
      private String firstName;
      private String lastName;
      private int age;
      private char sex;
      private String mailId;
      private String mobileNo;
      // Constructor
      public Student() {
      }
      // Constructor
      public Student(int studentId, long regNo, String firstName,
                  String lastName, int age, char sex, String mailId, String mobileNo) {
            this.studentId = studentId;
            this.regNo = regNo;
            this.firstName = firstName;
            this.lastName = lastName;
            this.age = age;
            this.sex = sex;
            this.mailId = mailId;
            this.mobileNo = mobileNo;
      }
      public int getStudentId() {
            return studentId;
      }
      public void setStudentId(int studentId) {
            this.studentId = studentId;
      }
      public long getRegNo() {
            return regNo;
      }
      public void setRegNo(long regNo) {
            this.regNo = regNo;
      }
      public String getFirstName() {
            return firstName;
      }
      public void setFirstName(String firstName) {
            this.firstName = firstName;
      }
      public String getLastName() {
            return lastName;
      }
      public void setLastName(String lastName) {
            this.lastName = lastName;
      }
      public int getAge() {
            return age;
      }
      public void setAge(int age) {
            this.age = age;
      }
      public char getSex() {
            return sex;
      }
      public void setSex(char sex) {
            this.sex = sex;
      }
      public String getMailId() {
            return mailId;
      }
      public void setMailId(String mailId) {
            this.mailId = mailId;
      }
      public String getMobileNo() {
            return mobileNo;
      }
      public void setMobileNo(String mobileNo) {
            this.mobileNo = mobileNo;
      }
      public static long getSerialversionuid() {
            return serialVersionUID;
      }

}

6.    Create class to run the sample application


import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;

import sample.hiber.crud.model.Student;

public class MainTest {
      private static SessionFactory factory;

      public static void main(String[] args) {
      MainTest m = new MainTest();
      try {
            factory = new Configuration().configure().buildSessionFactory();
      } catch (Throwable ex) {
      System.err.println("Failed to create sessionFactory object." + ex);
      throw new ExceptionInInitializerError(ex);
      }
      m.addStudent(new Student(1012012, 123123123l, "John", "Peter", 35, 'M',
      "pjohn@gmai.com", "5678902324"));
      }

      private void addStudent(Student s) {
            Session session = factory.openSession();
            session.beginTransaction();
            session.save(s);
            session.flush();
            session.getTransaction().commit();
            session.close();
      }

}