Hello friends,
Welcome back, today I am going to discuss the very important topic of hibernate as I promised. So let’s discuss get() and load() method of hibernate.
There is not much difference in code while working with either load() or get() method, But there are some minor difference between both method That helps you to handle get() and load() method problems so let us start the session:-
load() method
- It is used when you sure that object exists in the database.
- It will throw an exception of ObjectNotFoundException if the unique id is not found in the database.
- It returns a proxy object by default.
- It does not hit the database.
- It performs by default lazy loading. I have already define lazy loading in the previous session. if you don’t know what is lazy loading so please click here
- Let’s look at different types of load() method available in hibernate session:public Object load(Class theClass, Serializable id) throws HibernateException
public Object load(String entityName, Serializable id) throws HibernateException
public void load(Object object, Serializable id) throws HibernateException
Let see Example of the session.load():-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class Demo { public static void main(String[] args) { Configuration configuration=new Configuration().config("hibernate.cfg.xml"); // By default in config use hibernate.cfg.xml so that is not mandatry to write here. // if cfg file name is different then you have to put like above. Session session = configuration.buildSessionFactory().openSession(); Transaction transaction = session.beginTransaction(); transaction.begin(); Student student = (Student) session.load(Student.class, 1); //get employee object with id 1 using load() method System.out.println(Student); //if Student object found then update student course of retrieved the proxy object instead of original object. //That means it does not update to the database studdent.setCourse("Hibernate"); transaction.commit(); session.close(); sessionFactory.close(); System.out.println("Sucessfully updated!!!") } |
get() Method
- If you are not sure that the object exists or not, then use one of the get() methods.
- It will return null if the unique id is not found in the database.
- It will hit the database immediately.
- It returns the real object, not proxy object.
- Let’s look at different type of load() method available in hibernate session:
public Object get(Class className, Serializable id) throws HibernateException
public Object get(String entityName, Serializable id) throws HibernateException
Let see Example of the session.get() :-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
public class Test { public static void main(String[] args) { Configuration con=new Configuration().config(); Session session = con.buildSessionFactory().openSession(); Transaction transaction = session.beginTransaction(); transaction.begin(); Student student = (Student) session.get(Student.class, 1); //get student object with id 1 using get() method System.out.println(student); //update student course of retrieved object, //it can immediately update to the database because of get() method retrieved actualy persistent object not a proxy object. student.setCourse("Hibernate"); //Here we have updated object becuse we use get() method here. System.out.println(student); // It will give you of updated object details. transaction.commit(); session.close(); sessionFactory.close(); System.out.println("Sucessfully updated"); } } |
Now, I hope that all of you able to easily understood that difference between get() and load() method.
That’s all for this beginner level tutorial. In coming post, we will discuss more concepts around hibernate tutorial.
Happy Learning !!
Recent Comments