NHibernate Integrated Queries Code Review

Mapping the Class and Database Objects Using XML Mapping File

Code Review

Code Walkthrough

The following XML elements are used to define the key objects in the database model:

  • auto-import : whether or not unqualified class names (of classes in this mapping) can be used.
  • namespace : namespace to assume for unqualified class names in the mapping document.
  • assembly : assembly to assume for unqualified class names in the mapping document.
  • class : used to declare a persistent class.
  • class name : the .NET class name of the persistent class.
  • class table : the name of the database table being mapped to.
  • class lazy : whether or not lazy fetching is used to load the class.
  • id : used to declare the primary key column of the database table.
  • id name : name of the class identifier property.
  • id column name : name of the database table primary key column.
  • id generator : name of the .NET class used to generate unique identifiers for instances of the persistent class.
  • many-to-one : used to define a many-to-one association between persistent classes.
  • many-to-one name : name of the association.
  • many-to-one class : name of the associated class.
  • many-to-one column : name of the column to be used in the association.
  • many-to-one cascade : specifies which operations should be cascaded from the parent object to the associated object.
  • property : declares a persistent property of the class.
  • property name : name of the property of the persistent class.
  • property column : name of the mapped database table column.

The below XML is stored in the Person.hbm.xml file and is used to specify mapping between the Person data model of the database and the Person application object model.

<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" namespace="PersonNHibernateAdvanced_2_0" assembly="PersonNHibernateAdvanced_2_0">
<class name="Person" table="Person" lazy="true">
<id name="ID"> <column name="ID" /> <generator class="native" /> </id>
<many-to-one name="Role" class="Role" column="IDRole" cascade="save-update" />
<bag name="Salaries" cascade="all" lazy="true" inverse="true"> <key column="IDPerson" /> <one-to-many class="Salary" /> </bag>
<property name="LastName" column="LastName" /> <property name="FirstName" column="FirstName" />
</class> </hibernate-mapping>
  • bag : used to define a persistent collection.
  • bag name : the collection property name.
  • bag cascade : specifies which operations should be cascaded to the child entities.
  • bag lazy : whether or not lazy fetching is used. If disabled, association is eagerly fetched.
  • bag inverse : mark this collection as the inverse end of a bidirectional association.
  • bag key column : name of the foreign key column.
  • bag one-to-many : indicates a one to many association.

The below XML is stored in the Role.hbm.xml file and is used to specify mapping between the Role data model of the database and the Role application object model.

<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" namespace="PersonNHibernateAdvanced_2_0" assembly="PersonNHibernateAdvanced_2_0">
<class name="Role" table="Role" lazy="true">
<id name="ID"> <column name="id"/> <generator class="native" /> </id>
<set name="People" table="Person" cascade="all" inverse="true"> <key column="IDRole" /> <one-to-many class="Person" /> </set>
<property name="RoleDescription" column="RoleDescription" />
</class> </hibernate-mapping>
  • set : used to define a persistent collection.
  • set name : the collection property name.
  • set cascade : specifies which operations should be cascaded to the child entities.
  • set lazy : whether or not lazy fetching is used. If disabled, association is eagerly fetched.
  • set inverse : mark this collection as the inverse end of a bidirectional association.
  • set key column : name of the foreign key column.
  • set one-to-many : indicates a one to many association.

The below XML is stored in the Salary.hbm.xml file and is used to specify mapping between the Salary data model of the database and the Salary application object model.

<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" namespace="PersonNHibernateAdvanced_2_0" assembly="PersonNHibernateAdvanced_2_0">
<class name="Salary" table="Salary" lazy="true">
<id name="ID"> <column name="ID"/> <generator class="native" /> </id>
<many-to-one name="Person" class="Person" column="IDPerson" cascade="save-update" />
<property name="Year" column="Year" /> <property name="SalaryYear" column="SalaryYear" />
</class> </hibernate-mapping>