2

Here is my model class

@Entity
public class Student {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;
    private String name;
    private String company;
    private Date date;

    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", company=" + company + ", date=" + date + "]";
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getCompany() {
        return company;
    }
    public void setCompany(String company) {
        this.company = company;
    }
    public Date getDate() {
        return date;
    }
    public void setDate(Date date) {
        this.date = date;
    }

}

and here is main method

public class StudentTest {
    public static void main(String[] args) {
        SessionFactory sessionfactory=new Configuration().configure().buildSessionFactory();
        Session session=sessionfactory.openSession();

        Student student=new Student();
        student.setName("Rajat");
        student.setDate(new Date());
        student.setCompany("Yash");

        session.persist(student);
        student.setName("Prashant");

        Student getStudent=(Student)session.load(Student.class, 1);
        System.out.println("--------------load-------------");
        System.out.println(getStudent);

//      Student getStudent1=(Student)session.get(Student.class, 1);
//      System.out.println("-----------get------------------");
//      System.out.println(getStudent1);

        Transaction t=session.beginTransaction();

        session.getTransaction().commit();

        session.close();
        sessionfactory.close();
    }
}

I am trying to learn difference between (save and persist) and also (get and load method). here the load method gives exception while get method gives null.Why??

Prashant
  • 23
  • 5
  • This behaves exactly as documentation of both methods states. Have you tried to [read the documentation](https://docs.jboss.org/hibernate/orm/3.5/javadocs/org/hibernate/Session.html) at all? – M. Prokhorov Apr 20 '17 at 10:56

2 Answers2

0

As the javadoc for load underlines:

Return the persistent instance of the given entity class with the given identifier, assuming that the instance exists. ... Use this only to retrieve an instance that you assume exists, where non-existence would be an actual error.

This is not the case for the get method, which naturally allows non-existence and simply returns a null reference.

load is used for performance enhancing as there is no actual database query being fired until first usage of the entity (getter is triggered for example). The only downside is that you have to be sure that this particular entry actual exists in the database.

Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63
0

Well this is exactly the expected behaviour of both methods, you can simply see it in Hibernate Session documentation.

  • You get an exception with load() beacause it will throw an ObjectNotFoundException if no row is found.
  • While with get() you will get null if no row is found.

Documentation:

Documentation clearly says:

  • load(Class theClass, Serializable id):

Return the persistent instance of the given entity class with the given identifier, assuming that the instance exists.

  • get(Class theClass, Serializable id):

Return the persistent instance of the given entity class with the given identifier, or null if there is no such persistent instance.

Explanation:

You can see in this session.get() and session.load() Tutorial that:

  1. session.load() It will always return a “proxy” (Hibernate term) without hitting the database. In Hibernate, proxy is an object with the given identifier value, its properties are not initialized yet, it just look like a temporary fake object. If no row found , it will throws an ObjectNotFoundException.
  2. session.get() It always hit the database and return the real object, an object that represent the database row, not proxy. If no row found , it return null.
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
  • if session.get() always hits database then if i change above code to like this- `session.persist(student); Transaction t1=session.beginTransaction(); session.getTransaction().commit(); student.setName("Prashant"); Student getStudent1=(Student)session.get(Student.class, 1); System.out.println("-----------get------------------"); System.out.println(getStudent1); session.close(); sessionfactory.close();` why does it return Prashant instead of Rajat – Prashant Apr 20 '17 at 12:06
  • That's because the `student` object is cached into `Session` by `hibernate` so it isn't yet removed from the `session`, so `student` and `getStudent1` reference the same `Student` instance, you can get deeper into this subject from [**this answer**](http://stackoverflow.com/a/19872478/3669624). – cнŝdk Apr 20 '17 at 13:04