Monday 14 May 2012

Hibernate Persistence Operations



Persistance Operations in hibernate Environment
Single row Operations

1. save(
pojo obj)-------> to Insert record
2. persist(pojo obj)-----> to insert a record
3. merge(pojo obj)------>to Update record
4. update(pojo obj)---->to update record
5. saveOrUpdate(pojo obj)---->to insert or update a record
6. load(pojo obj,Identity value)----->to select a record
7. get(POJO obj,Identity value)------>to select a record
8. delete(pojo obj)---------->to delete a record


For Multiple Row Operations we can Use in 3 ways

1.Hibernate Query Language(HQL)
2.Native SQL
3.Criteria API



 What is difference between save and persist
Save and persist does an insert and will fail if the primary key is already persistent and both will raise Exceptions 
 session.save() returns the generated identifier (Serializable object) and session.persist() doesn't
 if you do :- 
         System.out.println(session.save(question)); 
         This will print the generated primary key. 
         if you do :- 
         System.out.println(session.persist(question)); 
         Compile time error because session.persist() return void.



What is difference between merge and update?
Merge method returns Object if object is not available in database it inserts.. Otherwise it update the row. merge method return id
Update method doesn’t return anything.. if object is not available in database it fails.. If row is existed it update the row
What is difference between load and get….
load()
--------------
1). Only use the load() method if you are sure that the object exists.
2). load() method will throw an exception if the unique id is not found in the database.
3). load() just returns a proxy by default and database won’t be hit until we call getter method.
get( )
---------
1). If you are not sure that the object exists, then use one of the get() methods.
2). get() method will return null if the unique id is not found in the database.
3). get() will hit the database immediately. 



To Select all rows from database
Configuration cfg=new Configuration();
cfg=cfg.configure();
SessionFactory sf=cfg.buildSessionFactory();
Session s=sf.openSession();
String st="from emp1";
Query q=s.createQuery(st);
List <emp1> obj=q.list();
Iterator<emp1> i=obj.iterator();
while(i.hasNext())
{
emp1 e=(emp1)i.next();
System.out.println("name is "+e.getEmpno());
}



Sessioin.flush() 
method forces the hibernate software to synchronize with underlying database software records by using the data of persistant state POJO class objects of persistence context.(with update class).
i.e.
Transaction t = session.beginTransaction();
session.update(obj);
t.commit();
Or
session.update(obj);
session.flush();




Session.refresh()

àre-reads data from DB table row into object for Synchronization…
Ex:
obj=(emp1)s.load(emp1.class, 1);
System.out.println("object no"+obj.getEmpno()+"name is "+obj.getEname());
System.out.println("sleeep.........");
try {
Thread.sleep(30000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
s.refresh(obj);
System.out.println("object no"+obj.getEmpno()+"name is "+obj.getEname());
}

Note:When it is sleep mode just update database  and you can see the effects..










No comments:

Post a Comment