Retrieve Data from table using Hibernate

Retrieve Data from table using Hibernate

We have already Created table Myphonebook .
Now in this example we are going to Retrieve data from that table.
Myphonebook table
createphonebook
Program to Retrieve data from table

[pastacode lang=”java” manual=”package%20com.spy.hibernate.First%3B%0Aimport%20org.hibernate.Session%3B%0Aimport%20org.hibernate.SessionFactory%3B%0Aimport%20org.hibernate.cfg.Configuration%3B%0A%0Apublic%20class%20RetrieveContact%20%0A%7B%0A%20public%20static%20void%20main(String%5B%5D%20args)%0A%20%20%7B%0A%20%20%20%20SessionFactory%20sf%3DnewConfiguration().configure().buildSessionFactory()%3B%0A%0A%20%20%20%20Session%20session%3Dsf.openSession()%3B%0A%0A%20%20%20%20session.beginTransaction()%3B%0A%0A%20%20%20%20%2F%2Fthis%20will%20Retrieve%20contact%20with%20index%201%0A%20%20%20%20Myphonebook%20contact2%3D%20(Myphonebook)session.get(Myphonebook.class%2C%201)%3B%0A%0A%20%20%20%20System.out.println(%22Contact%20name%20%3A%20%22%2Bcontact2.getName())%3B%0A%0A%20%20%20%20System.out.println(%22mobile%20Number%3A%20%22%2Bcontact2.getMobilenumber())%3B%09%09%0A%20%20%7D%09%0A%7D” message=”” highlight=”” provider=”manual”/]


<strong>Output:
</strong>Hibernate: select myphoneboo0_.id as id0_0_, myphoneboo0_.mobilenumber as mobilenu2_0_0_, 
           myphoneboo0_.name as name0_0_ from Myphonebook myphoneboo0_ where myphoneboo0_.id=?
 
 Contact name : bhavesh
 mobile Number: 8866766995

Leave a Reply