Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Hibernate Hibernate4 Hibernate get

Related Tutorials


 
 

Share on Google+Share on Google+

Hibernate get

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

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.

 

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 17, 2012

Related Tutorials

Discuss: Hibernate get  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com