In the last example we created contact.hbm.xml to map Contact Object to the Contact table in the database. Now let's understand the each component of the mapping file.
To recall here is the content of contact.hbm.xml:
"-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> |
Hibernate mapping documents are simple xml documents. Here are important elements of the mapping file:.
element
The first or root element of hibernate mapping document iselement. Between the <hibernate-mapping> tag class element(s) are present.
-
element
Theelement maps the class object with corresponding entity in the database. It also tells what table in the database has to access and what column in that table it should use. Within one element, several mappings are possible.
-
element
Theelement in unique identifier to identify and object. In fact element map with the primary key of the table. In our code :
primary key maps to the ID field of the table CONTACT. The attributes of the
id
element are:- name: The property name used by the persistent class.
- column: The column used to store the primary key value.
- type: The Java data type used.
- unsaved-value: This is the value used to determine if a class has been made persistent. If the value of the id attribute is null, then it means that this object has not been persisted.
element
The
* Increment - This is used to generate primary keys of type long, short or int that are unique only. It should not be used in the clustered deployment environment.
* Sequence - Hibernate can also use the sequences to generate the primary key. It can be used with DB2, PostgreSQL, Oracle, SAP DB databases.
* Assigned - Assigned method is used when application code generates the primary key.
-
element
Theproperty
elements define standard Java attributes and their mapping into database schema. Theproperty
element supports thecolumn
child element to specify additional properties, such as the index name on a column or a specific column type.
0 comments:
Post a Comment