- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between get() and load() in Hibernate
In hibernate, get() and load() are two methods which is used to fetch data for the given identifier. They both belong to Hibernate session class. Get() method return null, If no row is available in the session cache or the database for the given identifier whereas load() method throws object not found exception.
Sr. No. | Key | Get() | Load() |
---|---|---|---|
1 | Basic | It is used to fetch data from the database for the given identifier | It is also used to fetch data from the database for the given identifier |
2 | Null Object | It object not found for the given identifier then it will return null object | It will throw object not found exception |
3 | Lazy or Eager loading | It returns fully initialized object so this method eager load the object | It always returns proxy object so this method is lazy load the object |
4 | Performance | It is slower than load() because it return fully initialized object which impact the performance of the application | It is slightly faster. |
5. | Use Case | If you are not sure that object exist then use get() method | If you are sure that object exist then use load() method |
Example of Get() in Hibernate
@Entity public class User { @Id Integer id; String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import com.journaldev.hibernate.util.HibernateUtil; public class GetExample { public static void main(String[] args) { //get session factory to start transcation SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); //Get Example User user = (User) session.get(User.class, new Integer(2)); System.out.println("User ID= "+user.getId()); System.out.println("User Name= "+user.getName()); //Close resources tx.commit(); sessionFactory.close(); } }
Example of Load() in Hibernate
@Entity public class User { @Id Integer id; String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import com.journaldev.hibernate.util.HibernateUtil; public class LoadExample { public static void main(String[] args) { //get session factory to start transcation SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); //Load Example User user = (User) session.load(User.class, new Integer(2)); System.out.println("User ID= "+user.getId()); System.out.println("User Name= "+user.getName()); //Close resources tx.commit(); sessionFactory.close(); } }