Introducing Hibernate - Getting Started With Hibernate
(Page 3 of 4 )
ORM frameworks release the developer from the constraints of thinking in terms of relation while working with classes. There are many frameworks that come under this category. What makes Hibernate different from others is its simplicity and flexibility. To make one’s application Hibernate enabled, there are three prerequisites that need to be fulfilled, which are:
1. Persistence Class:
The persistence class is a Plain Old Java Object or POJO model. A POJO is comparable to a JavaBean, having getters and setters to access its properties that are the instance variables. The persistence class has the following characteristics:
a. It is the Object Oriented blueprint of the table to be persisted.
b. The attributes of the table become the instance variable of the persistence class.
c. The data type of the instance variables is the domain of the attributes.
d. The object of the persistence class represents the row of the table.
2. Mapping file:
The mapping file is an XML file that contains detailed mapping between the persistence class and the table it represents. The required elements of this XML file are:
a. hibernate-mapping:
Hibernate-mapping is the root element enclosing all other elements.
b. class:
Class is used to map the table name to the persistence class. The name attribute is used to specify the class name, and the table attribute is used to specify the table which the class represents. For example, to map a table named ORDERS to a persistence class with a fully qualified name of com.someorg.persist.Order the class element would be:
<class name=”com.someorg.persist.Order” table=”ORDERS”>…</class>
c. id:
This element is used to map the primary key of the table to an instance variable of the class. The column child element of the id can be used to map the primary key to the corresponding variable. Whether the value of the primary key can be automatically generated is declared here. The generator element can be used to tell Hibernate whether a class would be used to auto generate the ids, or that the ids are being assigned by the application. The most common approach is assigned. For example:
<id name="id" type="string"
unsaved-value="null">
<column name="id" sql-type="char(32)"
not-null="true"/>
<generator class="assigned"/>
</id>
tells Hibernate that the name of the primary key is “id” which is mapped to the instance variable “id”. The data type of the variable is “string” which is mapped to the column “id” having Domain as “char(32)” represented by the sql-type attribute. The generator’s class attribute contains the value “assigned” meaning that the application itself will provide the value of the primary key.
d. property:
This element, along with its child element column, maps the other attributes (or columns) to the instance variables of the persistence class. The name attribute of the property contains the name of the variable as a value. The name attribute of the column element contains the name of the column to which the instance variable is mapped. The length and sql-type attributes point to the length and data type of the column. To cite an example:
<property name="name">
<column name="name" sql-type="char(255)"
not-null="true"/>
</property>
maps the Column “name” to instance variable “name”.
3. Hibernate Configuration file:
This file can be called the entry point to a Hibernate enabled application. The reason is that it contains the configuration that controls the communication with the underlying database. It is here that all the mapping files being used by the application are declared. The hibernate-configuration element forms the root element. The most common elements used in this XML file are:
a. property:
The name attribute of the property element can be used to configure various parameters. In the terminology of Hibernate, these parameters are known as property parameters. Database server type (MySQL, Oracle, and so on), dialect class, URL of the database, username/password et. al. are the various property parameters. To put it in code:
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:data/tutorial</property>
<property name="connection.username">sa</property>
:
:
</session-factory>
b. mapping:
It is through the mapping element that all the mapping files, also called hbm files, to be used by the application are declared. The resource attribute is given the value of the path to the hbm file. To elucidate:
<mapping resource="Event.hbm.xml"/>
That is all one needs to get started with Hibernate. In the next section I will put it all together to provide a view into the real world of Hibernate.
Next: Hibernate in the Real World >>
More Java Articles
More By A.P.Rajshekhar