I am working on hibernate from the past 1 year and i shared my knowledge which i gain. I referred Java Persistent And Hibernate book.
First Hibernate Application
First of all, download the latest production release of hibernate from http://www.hibernate.org/ . These are the jar file to execute a hibernate project.
hsqldb.jar is related to HSQLDB distribution . you can replace it for different driver jar if you want too use different database management system. It may be to replace the some jar from its latest release.
Create the Domain model For ITEM
Item persistent class will mapped with ITEM database table. I am trying to save the item name and its amount in the ITEM database table. id property represents the primary key in ITEM table.
Mapping of model class with database table
Hibernate needs some information about how the model classes should be made persistent. Hibernate need to know how persistent classes should be stored and loaded. We defines in XML mapping document that describes how to properties of Item class or models should be mapped with ITEM table in database.
Create the Item.hbm.xml file in the same package where you model classes has defined.
Following XSD for this metadata file is required for hibernate elements.
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">.
Hibernate.cfg.xml
Following XSD for this hibernate.cfg.xml is required for hibernate-cofiguration element. I defined about this file in my next post.
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
Now, time to store the item name and its amount in ITEM database table.
HibernateUtil File
This class creates a hibernate session factory object on startup the application.
configuration object search the hibernate.properties file on class path and initialise the all property which have mentioned in this file. configure method of configuration object search the hibernate.cfg.xml file on the class path and if it find the one than it override the properties which have declared same as hibernate.properties file else it throw an exceptionInitializationError by wrapping it exception object. try catch blog is mandatory in static block.
Log4j.properties file
Following Insert statement is execute when you try to run this Test.java.
Hibernate: insert into ITEM (Item_NAME, ITEM_AMOUNT, CREATED_DATE, MODIFIED_DATE, ITEM_ID) values (?, ?, ?, ?, ?).