Hibernate Mapping Basics
Hibernate mappings are used to map a Java object to database tables. This mapping technology has following features.
– XML a universally accepted configuration technique is used for mapping
– Java centric as the mapping is constructed around Java objects. Because of this a Java developer can write code for database interaction without knowing much about SQL
– Flexibility in organization of mapping files. One can have mapping file per class or can group classes in one mapping file logically.
Many tools are available in market for generation of mapping files, e.g. XDoclet, MiddleGen, etc.
Mapping files can also be used to generate the object code which can reduce the development cost.
Following entities are involved in hibernate mapping:
Persistent Classes
Database Tables
Mapping Files
Persistent Classes:
The persistent classes are POJOs (plain old Java objects), but following points should be remembered for these objects.
– Default constructor is required as Hibernate uses it to instantiate the class
– Prefer non final classes, as lazy loading does not work with final classes.
Database Table:
We are using hibernate to map an object with database table. Following table can be a table required for above object.
Table name: CAT_TB
Id – Number(10)
Name – Varchar2(100)
Weight – Number (12,2)
Hibernate Mapping:
Below is the mapping xml for above object and table.
Important Terms:
Following important terms must be understood in mapping -
Id:
This element represents the primary key of the object. Value of this element depends on the generator element value. The ids can be mapping to single database field or these can be combination of two or more fields. Such ids are called composite ids.
Generators:
Generators implement org.hibernate.id.IdentifierGenerator and these are the built in generators increment, identity, sequence, hilo, seqhilo, uuid, guid, native, assigned, select, foreign. Generator element value tells Hibernate how the id element value is going to be populated.
The ‘assigned’ generator class allows application to set the id, and hibernate uses this id while persisting the object in database.
Example:
Mostly in applications, the id field is generated using a sequence. Here is an example of this generation.
<id name=”id” column=”CAT_ID”>
<generator class=”sequence”>
<param name=”sequence”>SQ_CAT_ ID</param>
</generator>
</id>
Discriminator:
It is required in polymorphic object persistence i.e. when there is one table for class persistence. E.g. Cards table for to store credit card and debit cards. Value of this field will be used to differentiate the row. Such storage strategy is called as table-per-class-hierarchy strategy.
Version and Timestamp:
These fields are used to handle concurrent access of data. These are mainly used in case of long transactions where detached objects are involved. E.g. Application retrieves data and passes detached object to screen, the object contains certain value of version, when the object is again tried to persist after few screen operations, the version or time stamp fields can be used to check if the database row is modified after read. If modified the appropriate action can be taken, otherwise the data can be persisted.
Association Mappings:
Following types mapping possibilities are available in hibernate.
By Direction:
Unidirectional mapping
Bi-directional mapping
By Multiplicity:
Many to One
One to Many
One to One
Many to Many
These advance mappings are discussed in future ‘Advance Hibernate Mappings’ article.









Leave your response!