Hibernate - SELECT Operation
We have following methods to perform SELECT Operation in Hibernate
Object get(Class , Serializable id)
Object load(Class, Serializable id) throws ObjectNotFoundException
Parameter | get | load |
---|---|---|
Database retrieval | It always hits the database | It does not hit database |
Proxy | It returns real object | It returns proxy object |
Id not found | If it does not get the object with id, it returns null | If it does get the object with id, it throws ObjectNotFoundException |
Use | If you are not sure if object with id exists or not, you can use get | If you are sure about existence of object, you can use load |
Example
public class EmployeeSelect {
public static void main(String[] args) {
//1.Load Configuration
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
//2.Create Session
SessionFactory sf = cfg.buildSessionFactory();
Session session = sf.openSession();
//3.Perform Operations
Object ob = session.load(EmployeeBo.class, new Integer(1));
EmployeeBo bo = (EmployeeBo) ob;
System.out.println("SELECTED DATA\n ================");
System.out.println("EID : "+bo.getEid());
System.out.println("NAME : "+bo.getName());
System.out.println("ADDRESS : "+bo.getAddress());
}
}
------------------------------------------------------
log4j:WARN No appenders could be found for logger org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select employeebo0_.eid as eid0_0_, employeebo0_.name as name0_0_, employeebo0_.address as address0_0_ from employee employeebo0_ where employeebo0_.eid=?
SELECTED DATA
================
EID : 1
NAME : Satya
ADDRESS : VIJYAYAWADA
Get vs load
public class App {
public static void main(String[] args) {
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
System.out.println("get : Database retrieval");
Student bo1 = (Student) session.get(Student.class, new Integer(101));
Student bo2 = (Student) session.get(Student.class, new Integer(101));
System.out.println("get : ID not found.... ");
Student bo3 = (Student) session.get(Student.class, new Integer(102));
System.out.println(bo3);
System.out.println("load : Database retrieval");
Student bo4 = (Student) session.load(Student.class, new Integer(101));
Student bo5 = (Student) session.load(Student.class, new Integer(101));
System.out.println("load : ID not found.... ");
Student bo6 = (Student) session.load(Student.class, new Integer(102));
System.out.println(bo6);
}
}
---------------------------------------------------------------
get : Database retrieval
Hibernate: select student0_.sno as sno0_0_, student0_.name as name0_0_, student0_.address as address0_0_ from student student0_ where student0_.sno=?
Hibernate: select student0_.sno as sno0_0_, student0_.name as name0_0_, student0_.address as address0_0_ from student student0_ where student0_.sno=?
//Two times it hits the Database
get : ID not found....
Hibernate: select student0_.sno as sno0_0_, student0_.name as name0_0_, student0_.address as address0_0_ from student student0_ where student0_.sno=?
null
//No Error , it showing null
load : Database retrieval
//data with same id already loaded at get() call, it just returns Proxy Object
load : ID not found....
Hibernate: select student0_.sno as sno0_0_, student0_.name as name0_0_, student0_.address as address0_0_ from student student0_ where student0_.sno=?
Exception in thread "main" org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [rewrite.Student#102]
PREVIOUSCURD Operations
NEXTINSERT Operation