How to store object in Hibernate?
This example shows you how to store or save Hibernate object to database. The basic steps in creating application in Hibernate will be:
- Creates the POJO
- Create the Hibernate mapping file
- Register the mapping file in the Hibernate configuration
- Create a simple manager class to store the object
In this example we'll create a class called
Label, this class is about a record label company. This class has the
modifiedData attributes. Their types in order are
Hibernate is an Object Relation Mapping technology which basically means how an object is mapped to a relational database model. Because of this it needs a mapping file to map the object properties to table columns. The mapping file usually named in the format of
Label.hbm.xml, the class name with
hbm.xml suffix. And for Hibernate application recognize the object, the mapping file should be registered in the Hibernate configuration
We have a brief introduction about the Hibernate class and configuration structure. Let's jump the the working example. First we creates the mapping file and then we create the classes.
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
<class name="com.javacoderanch.example.hibernate.app.Label" table="LABELS">
<id name="id" column="id">
<property name="name" not-null="true"/>
<property name="modifiedBy" column="modified_by" length="50"/>
<property name="modifiedDate" column="modified_date" type="timestamp"/>
We have the code and the mapping file done. To register the mapping file in the Hibernate configuration file you can see the How to create Hibernate's SessionFactory? example. The example also tells you how to create the
SessionFactoryHelper class to obtain the Hibernate's