Hibernate - get() and load() Method Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report Hibernate is a Java framework that provides a powerful set of tools for persisting and accessing data in a Java environment. It is often used in conjunction with Spring. Spring and Hibernate are both widely used in the Java community, and they can be used together to build powerful and efficient Java-based applications. Let's deal with Fetching objects in Spring Hibernate:Hibernate provides different methods to fetch data from the database. get()load()1. get() methodget() method is used to retrieve a persistent object from the database. It is a member of the Session interface, and it takes the class of the object to be retrieved and the primary key of the object as arguments.get() method only hits the database if the object is not present in the session cache. If the same object is already present in the cache then it returns previously stored data from the cache.get() method returns null if there is no object present in the database.Example: Java // Open a session Session session = sessionFactory.openSession(); // Begin a transaction Transaction transaction = session.beginTransaction(); // Retrieve the object using the primary key Customer customer = (Customer) session.get(Customer.class, 1L); // Commit the transaction transaction.commit(); // Close the session session.close(); In this example, the get() method is used to retrieve a Customer object with a primary key of 1. The object is then cast to the Customer class and stored in the custom variable. 2. load() methodload() method is used to retrieve an object from the database by its identifier (primary key). It is used to initialize a proxy object instead of a fully-initialized object, so it can be used to lazily load the object when it is needed.load() method does not retrieve the object from the database when it is called. Instead, it returns a proxy object that represents the object. The actual object is only retrieved from the database when it is needed, such as when a method of the object is called or a property is accessed. This technique is known as "lazy loading" and it is used to improve the performance of Hibernate by avoiding unnecessary database queries.load() method throws ObjectNotFoundException if there is no object found in the database.Example: Java Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); // Load the entity with the identifier 1 Employee employee = (Employee) session.load(Employee.class, 1L); // Print out the employee's name System.out.println(employee.getName()); transaction.commit(); session.close(); In this example, the load() method is used to retrieve an Employee object with a primary key of 1. The object is then cast to the Employee class and stored in the employee variable and printed the employee object. Create Quiz Comment P pranay0911 Follow 0 Improve P pranay0911 Follow 0 Improve Article Tags : Java Technical Scripter Technical Scripter 2022 Java-Hibernate Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like