3.CURD Operations


CURD Operations

1. Insert
·         int    - s.save(ob)
·         void - persist(ob)
·         void - saveorupdate(ob)

Serializable save(Object object)
Persist the given transient instance, first assigning a generated identifier.

void saveOrUpdate(Object object)
Either save(Object) or update(Object) the given instance.

2. Update
·         void - update(ob)  : cannot update complete column. Not pkey update
·         void - merge(ob) : get or duplicate obj data to be update

3. Delete
·         void - delete(ob)

4. Select - Single row select Only
·         Object - get(Entity.class, 101)  :Eger Select :  no id exception
·         Object - load(Entity.class,101) :Lazy Select : no id no exception


1. Insert Example

package dao;

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

import bo.StudentBo;

public class StudentDao {
public static void main(String[] args) {
       Configuration cfg = new Configuration();
       cfg.configure("res/hibernate.cfg.xml"); 
      
       SessionFactory factory = cfg.buildSessionFactory();
       Session session = factory.openSession();
       Transaction transaction = session.beginTransaction();
      
       System.out.println("***  1. Insert : serialize    - s.save(ob) ********************************");
       StudentBo bo1 = new StudentBo();
       bo1.setId(101);
       bo1.setName("SRIRAM");
       bo1.setAddress("HYDERABAD");     
       session.save(bo1);
      
       System.out.println("***  2. Insert : void    - s.persist(ob) ********************************");
       StudentBo bo2 = new StudentBo();
       bo2.setId(102);
       bo2.setName("ANJI");
       bo2.setAddress("VIJAYAWADA");    
       session.persist(bo2);
      
       System.out.println("***  3. Insert : void    - s.saveorUpdate(ob) ********************************");
       StudentBo bo3 = new StudentBo();
       bo3.setId(103);
       bo3.setName("VINOD");
       bo3.setAddress("HOOBLY"); 
       session.saveOrUpdate(bo3);
      
      
       transaction.commit();     
       session.close();
       factory.close();

}
}


2. Update Example

package dao;

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

import bo.StudentBo;

public class StudentDao {
public static void main(String[] args) {
       Configuration cfg = new Configuration();
       cfg.configure("res/hibernate.cfg.xml"); 
      
       SessionFactory factory = cfg.buildSessionFactory();
       Session session = factory.openSession();
       Transaction transaction = session.beginTransaction();
      
       System.out.println("***  1. Update  ********************************");
       StudentBo bo1 = new StudentBo();
       bo1.setId(101);
       bo1.setName("xxx");
       bo1.setAddress("xxxxx");  
       session.update(bo1);
      
       System.out.println("***  2. UPdate ********************************");
       StudentBo bo2 = new StudentBo();
       bo2.setId(102);
       bo2.setName("yyyy");
       bo2.setAddress("yyyy");   
       session.merge(bo2); 
      
       transaction.commit();     
       session.close();
       factory.close();    
}
}



3. Select and Delete Example

package dao;

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

import bo.StudentBo;

public class StudentDao {
public static void main(String[] args) {
       Configuration cfg = new Configuration();
       cfg.configure("res/hibernate.cfg.xml"); 
      
       SessionFactory factory = cfg.buildSessionFactory();
       Session session = factory.openSession();
       Transaction transaction = session.beginTransaction(); 
       System.out.println("***  1. select  ********************************");
       StudentBo bo = (StudentBosession.get(StudentBo.class, 101);
       System.out.println(" ID : "+bo.getId()+"  NAME : "+bo.getName()+"  : "+bo.getAddress());
      
       System.out.println("***  2. Load Select  ********************************");
        bo = (StudentBosession.load(StudentBo.class, 102);
        System.out.println(" ID : "+bo.getId()+"  NAME : "+bo.getName()+"  : "+bo.getAddress());
        
        
        System.out.println("***  3. Delete   ********************************");
        session.delete(bo);
        
        transaction.commit();
       session.close();
       factory.close();    
}
}