Hibernate-Hello World

Lets start with  first program in hibernate with saving an object into the database .While using hibernate we save the object in database and not the record like in JDBC

Below are the files we require to shape an hibernate program..

  • Product.java (My POJO class)
  • Product.hbm.xml  (Xml mapping file )
  • hibernate.cfg.xml  (Xml configuration file)
  • ClientForSave.java (java file to write our hibernate logic)

Product.java:

Next is our xml file

Product.hbm.xml:

In the above mapping file,  Product class is linked with PRODUCTS table in the database, and next is the id element, means in the database table what column we need to take as primary key column, that property name we need to give here, here property name productId is mapped with pid column in the table.
And proName is mapped with pname column of the PRODUCTS table, we have not specified any column for the property price, this means that, our property name in the pojo class and the column name in the table both are same.

Remember: the first 3 lines is the DTD for the mapping file, as a programmer no need to remember but we need to be very careful while you are copying this DTD, program may not  be executed if you write DTD wrong, actually we have separate DTD’s for Mapping xml and Configuration xml files.

hibernate.cfg.xml:

In this configuration file  Oracle database connection properties are used ,in case your are using any other database corresponding properties need to be replaced.

ClientForSave.java

Now compile all .java files and run ClientForSave.java and check the output

Output Eclipse

Note:

  • Make sure all .class, .java, .xml files are exist in the same folder
  • Before you compile and run this application, ensure you set the class path for all 12 jars files,
  • except select operation, all other operations must be in the Transaction Scope

 

   

Comments are closed