Book Image

NHibernate 2 Beginner's Guide

By : Aaron Cure
Book Image

NHibernate 2 Beginner's Guide

By: Aaron Cure

Overview of this book

<p>NHibernate is an open source object-relational mapper, or simply put, a way to retrieve data from your database into standard .NET objects. Quite often we spend hours designing the database, only to go back and re-design a mechanism to access that data and then optimize that mechanism. This book will save you time on your project, providing all the information along with concrete examples about the use and optimization of NHibernate.<br /><br />This book is an approachable, detailed introduction to the NHibernate object-relational mapper and how to integrate it with your .NET projects. If you're tired of writing stored procedures or maintaining inline SQL, this is the book for you.<br /><br />Connecting to a database to retrieve data is a major part of nearly every project, from websites to desktop applications to distributed applications. Using the techniques presented in this book, you can access data in your own database with little or no code.<br /><br />This book covers the use of NHibernate from a first glance at retrieving data and developing access layers to more advanced topics such as optimization and Security and Membership providers. It will show you how to connect to multiple databases and speed up your web applications using strong caching tools. We also discuss the use of third-party tools for code generation and other tricks to make your development smoother, quicker, and more effective.</p>
Table of Contents (19 chapters)
NHibernate 2
Credits
About the Author
About the Reviewers
Preface
Index

The XML mapping file (hbm.xml)


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>