Wednesday, November 18, 2015

Hibernate Select Query

Hibernate Select Query

This is an example for loading the object from the database remember in the hibernate loading 1 object from the database means applying select command (select * from _____) for fetching one complete record from the database.
Files required to execute this program..
  • Product.java (My POJO class)
  • Product.hbm.xml  (Xml mapping file )
  • hibernate.cfg.xml  (Xml configuration file)
  • ClientProgram.java(java file to write our hibernate logic)

Product.java (POJO)


Product.hbm.xml

hibernate.cfg.xml


ClientProgram.java

Now compile all .java files and run ClientProgram.java to see the output

Eclipse Output


In the database

Note:

  • In this program Product.java is just pojo class nothing special
  • Mapping and Configuration files are just like previous programs
  • But in ClientProgram.java, see in line number 16 load(-,-) method which is in the session, actually we have 2 methods to load the object from the database, they are load and get i will explain when time comes, as of now just remember this point
  • Now see line number 19,  we are going to print the product name by writing +s.getProName
  • Actually once we loaded the object for the database with load or get methods the object data will be loads into the Product.java(POJO) setter methods, so we are printing by using getter methods

No comments:

Easy Way to Handle Android Notifications

Android Notifications Android Toast class provides a handy way to show users alerts but problem is that these alerts are not persist...