Hibernate Tutorial

Testing (HibernateExample.java)

package com.javatips.student;

import java.util.List;
import java.util.Iterator;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.javatips.util.HibernateUtil;

public class HibernateExample {

 
public static void main(String[] args) {
   
HibernateExample example = new HibernateExample();
    Long studentId2 = example.saveStudent
("Sumith");
    Long studentId3 = example.saveStudent
("Anoop");
    example.listStudent
();
    example.updateStudent
(studentId3, "Rockey");
    example.deleteStudent
(studentId2);
    example.listStudent
();

 
}

 
public Long saveStudent(String studentName) {
   
Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction =
null;
    Long studentId =
null;
   
try {
     
transaction = session.beginTransaction();
      Student student =
new Student();
      student.setStudentName
(studentName);
      studentId =
(Long) session.save(student);
      transaction.commit
();
   
} catch (HibernateException e) {
     
transaction.rollback();
      e.printStackTrace
();
   
} finally {
     
session.close();
   
}
   
return studentId;
 
}

 
public void listStudent() {
   
Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction =
null;
   
try {
     
transaction = session.beginTransaction();
     
@SuppressWarnings("unchecked")
     
List<Student> Students = session.createQuery("from Student").list();
     
for (Iterator<Student> iterator = Students.iterator(); iterator.hasNext();) {
       
Student student = (Student) iterator.next();
        System.out.println
(student.getStudentName());
     
}
     
transaction.commit();
   
} catch (HibernateException e) {
     
transaction.rollback();
      e.printStackTrace
();
   
} finally {
     
session.close();
   
}
  }

 
public void updateStudent(Long studentId, String studentName) {
   
Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction =
null;
   
try {
     
transaction = session.beginTransaction();
      Student student =
(Student) session.get(Student.class, studentId);
      student.setStudentName
(studentName);
      transaction.commit
();
   
} catch (HibernateException e) {
     
transaction.rollback();
      e.printStackTrace
();
   
} finally {
     
session.close();
   
}
  }

 
public void deleteStudent(Long studentId) {
   
Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction =
null;
   
try {
     
transaction = session.beginTransaction();
      Student student =
(Student) session.get(Student.class, studentId);
      session.delete
(student);
      transaction.commit
();
   
} catch (HibernateException e) {
     
transaction.rollback();
      e.printStackTrace
();
   
} finally {
     
session.close();
   
}
  }
}

Output

Hibernate: insert into StudentDetails (STUDENT_NAME) values (?)
Hibernate: insert into StudentDetails (STUDENT_NAME) values (?)
Hibernate: select student0_.STUDENT_ID as STUDENT1_0_, student0_.STUDENT_NAME as STUDENT2_0_ from StudentDetails student0_
Sumith
Anoop
Hibernate: select student0_.STUDENT_ID as STUDENT1_0_0_, student0_.STUDENT_NAME as STUDENT2_0_0_ from StudentDetails student0_ where student0_.STUDENT_ID=?
Hibernate: update StudentDetails set STUDENT_NAME=? where STUDENT_ID=?
Hibernate: select student0_.STUDENT_ID as STUDENT1_0_0_, student0_.STUDENT_NAME as STUDENT2_0_0_ from StudentDetails student0_ where student0_.STUDENT_ID=?
Hibernate: delete from StudentDetails where STUDENT_ID=?
Hibernate: select student0_.STUDENT_ID as STUDENT1_0_, student0_.STUDENT_NAME as STUDENT2_0_ from StudentDetails student0_
Rockey

 





Previous 1 2 3








Your email address will not be published. Required fields are marked *