Hibernate get

In this section, you will learn about get method of Hibernate.

Hibernate get

Hibernate get

In this section, you will learn about get method of Hibernate.

The method get() is usually confused with load() method. Both method get() and load() is used to retrieve an object through different mechanism.

The method get() returns the real object fetched directly from the database. The method get() can be used as follows :


Worker worker = (Worker)session.get(Worker.class, new Integer(2));
WorkerDetail workerDetail = new WorkerDetail();
//set detail of workerDetail 
workerDetail.setWorker(worker); 
session.save(workerDetail);

The method load() returns proxy object which is a temporary fake object, which may or may not exist in database. The method load()  can be used as follows :


Worker worker = (Worker)session.load(Worker.class, new Integer(2));
WorkerDetail workerDetail = new WorkerDetail();
//set detail of workerDetail
workerDetail.setWorker(worker); 
session.save(workerDetail);

If the proxy object's related record is not existed in the database and you tried to retrieve its property from database table, it will throw ObjectNotFoundException .

The get() method return  null, if you try to fetch the property which is not existed in the database.