Thursday, 30 August 2018

Important terms of hyernate

1.configuration ->create configuration object
2.sessionFactory ->create session factory object on configuration object.
3.create session object on sessionFactory object.
4.On session object open the session for query.
5.on session object call begainTransaction
6.Perform query on objet.
7.then call transaction commit.
8.then cose session .
9.then close session factory object.

Hare I mention simple Hybernate example.


package decodejava;

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

public class Hiber
{

public static void main(String[] args)
{

//Creating the Configuration object.
Configuration cfg = new Configuration().configure("hibernate.cfg.xml").addResource("userdata.hbm.xml");

//Using the Configuration object to creagte a SessionFactory object.
SessionFactory sf = cfg.buildSessionFactory();

//Using the SessionFactory object to create a Session object.
Session session = sf.openSession();

//Creating the first object
UserData ob1 = new UserData();
ob1.setId(1);
ob1.setName("Adam");

//Creating the second object
UserData ob2 = new UserData();
ob2.setId(2);
ob2.setName("Ahmad");

//Creating the third object
UserData ob3 = new UserData();
ob3.setId(3);
ob3.setName("Amit");


session.beginTransaction();
session.save(ob1); //Saving the first object.
session.save(ob2); //Saving the second object
session.save(ob3); //Saving the third object
session.getTransaction().commit();
session.close();


ob1 = null;

//Creating a new Session to retrieve and modify the object.
session= sf.openSession();

session.beginTransaction();

ob1 =(UserData)session.get(UserData.class, 3);

//Modifying the username of Id=3
ob1.setName("Rohan");

//Saving the modified object in database
session.saveOrUpdate(ob1);

//Committing the trasaction
session.getTransaction().commit();

//Closing the session
session.close();


ob1 = null;
ob2 = null;
ob3 = null;


//Creating a new Session to retrieve the objects.
session= sf.openSession();
session.beginTransaction();
ob1 = (UserData)session.get(UserData.class,1);
ob2 = (UserData)session.get(UserData.class,2);
ob3 = (UserData)session.get(UserData.class,3);
System.out.println("Retrieving the saved objects");


//Retrieving details of the first user.
System.out.println("First User");
System.out.println("Id : " + ob1.getId() + "  Name : " + ob1.getName());


//Retrieving details of the second user.
System.out.println("Second User");
System.out.println("Id : " + ob2.getId() + "  Name : " + ob2.getName());

//Retrieving details of the third user.
System.out.println("Third User");
System.out.println("Id : " + ob3.getId() + "  Name : " + ob3.getName());


session.getTransaction().commit();

//closing the session
session.close();

//closing the heavyweight SessionFactory object
sf.close();

}
}

Importan method to remember

1. Configuration(). to create the configuration object to configure hybernet setting(configuration file ) and mapping document.





No comments:

Post a Comment