The following code snippet shows the Login.hbm.xml
mapping file for this simple table, with all the information required not only to map the data, but also to create the database from the metadata contained within the mapping file. If we do not want to be able to generate the database from the mapping file, then we can omit all of the sql-type
, unique
, and index
properties.
Some immediate information you might pick up from the file are the name of the class that NHibernate will use to map database rows (BasicWebApplication.Common.DataObjects.Login
), which is defined in the <class>
tag. This says that the BasicWebApplication.Common.DataObjects.Login
object is contained in the BasicWebApplication
assembly. It further defines that the Login
table is the database table we will be mapping to, using the <table>
element.
There is an <id>
tag that defines what the unique identifier (ID) is for the database record, as well as how that identifier is expected to be created. In our case, the <generator class="hilo">
tag specifies that we will be using the hi/lo Persistent Object ID (POID) generator for IDs.
The four string fields FirstName
, LastName
, UserName
, and Password
are then mapped to the four database columns of the same names, using the <property>
tag.
<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="BasicWebApplication.Common.DataObjects"assembly="BasicWebApplication"> <class name="Login" table="Login"> <id name="Id" type="Int32" unsaved-value="null"> <column name="Id" /> <generator class="hilo" /> </id> <property name="FirstName" type="String" /> <property name="LastName" type="String" /> <property name="UserName" type="String" /> <property name="Password" type="String" /> </class> </hibernate-mapping>