In Hibernate Object/relational mappings are normaly defined in an XML document. The xml documant for mapping is designed for readable and hand-editable. Which language we use for mapping that is Java-centric, It meaning that mappings are constructed around persistent class declarations and not table declarations.
We have to remember many Hibernate users chose to write the XML by hand, for doing this a number of tools exist to generate the mapping document. These tools include XDOclet,Middlegen and AndroMDA.
To unserstand the above conversation we take a example which is below:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="eg">
<class name="Cater"
table="cats"
discriminator-value="C">
<id name="id">
<generator class="native"/>
</id>
<discriminator column="subclass"
type="character"/>
<property name="weight"/>
<property name="birthdate"
type="date"
not-null="true"
update="false"/>
<property name="color"
type="eg.types.ColorUserType"
not-null="true"
update="false"/>
<property name="sex"
not-null="true"
update="false"/>
<property name="litterId"
column="litterId"
update="false"/>
<many-to-one name="mother"
column="mother_id"
update="false"/>
<set name="kittens"
inverse="true"
order-by="litter_id">
<key column="mother_id"/>
<one-to-many class="Cat"/>
</set>
<subclass name="DomesticCat"
discriminator-value="D">
<property name="name"
type="string"/>
</subclass>
</class>
<class name="Dog">
<!-- mapping for Dog could go here -->
</class>
</hibernate-mapping>
To understand the tag we need to go through the given table:
Tag
|
Descripton
|
Name
|
Java class name of the persistence class or interfaces
|
Table
|
Name of database table
|
discriminator-value (optional - defaults to the class name):
|
A value that distiguishes individual subclasses, used for
polymorphic behaviour. Acceptable values include null and not
null.
|
property-ref (optional)
|
the name of a property of the associated class that is joined
to this foreign key. If not specified, the primary key of the
associated class is used.
|
Id
|
Each entity bean always will have a primary key, which
annotated on the class with the Id annotation. The primary key can
be a single field or a combination of multiple fields depending on
table structure.
|
We will now discuss the content of the mapping document. We will only describe, however, the document elements and attributes that are used by Hibernate at runtime. The mapping document also contains some extra optional attributes and elements that affect the database schemas exported by the schema export tool (for example, the not-null attribute).