NHibernate 2 Beginner's Guide

NHibernate 2 Beginner's Guide
eBook: $26.99
Formats: PDF, PacktLib, ePub and Mobi formats
save 15%!
Print + free eBook + free PacktLib access to the book: $71.98    Print cover: $44.99
save 37%!
Free Shipping!
UK, US, Europe and selected countries in Asia.
Also available on:
Table of Contents
Sample Chapters
  • Incorporate robust, efficient data access into your .Net projects
  • Gain database independence, not tied to any particular technology
  • Avoid spending countless hours developing data access layers
  • Eliminate writing stored procedures
  • Clear, precise step-by-step directions to get up and running quickly

Book Details

Language : English
Paperback : 276 pages [ 235mm x 191mm ]
Release Date : May 2010
ISBN : 1847198902
ISBN 13 : 9781847198907
Author(s) : Aaron Cure
Topics and Technologies : All Books, Application Development, Open Source

Table of Contents

Chapter 1: First Look
Chapter 2: Database Layout and Design
Chapter 3: A Touch of Class
Chapter 4: Data Cartography
Chapter 5: The Session Procession
Chapter 6: I'm a Logger
Chapter 7: Configuration
Chapter 8: Writing Queries
Chapter 9: Binding Data
Chapter 10: .NET Security
Chapter 11: It's a Generation Thing
Chapter 12: Odds and Ends
Appendix: Pop Quiz Answers
  • Chapter 1: First Look
    • What is NHibernate?
    • Why would I use it?
    • Where do I get it?
    • Can I get help using NHibernate?
    • Database table
    • The XML mapping file (hbm.xml)
    • Plain Old CLR Object ( POCO )
    • Data access
    • Look how easy it is to use!
    • Summary
  • Chapter 2: Database Layout and Design
    • Before you get started
    • Laying the foundation—table layouts
    • Time for action – creating an ordering system database
    • Table layouts
    • NHibernate assigned IDs
    • Relationships
    • Normal Form
    • Putting it all together
    • Time for action – adding some tables to our Ordering system database
    • Summary
  • Chapter 3: A Touch of Class
    • Start up our applications
    • Creating objects
    • Time for action – creating our first class
    • Public properties and private variables
    • Time for action – adding a few properties
    • Converting SQL database types to .NET types
    • Properties for Foreign Keys
    • Summary
  • Chapter 4: Data Cartography
    • What is mapping?
    • Types of mapping
    • XML mapping
      • Getting started
      • Classes
      • Properties
      • ID columns
    • Mapping our types
    • Time for action – mapping basic types
    • Relationships
      • One-to-many relationships
      • Many-to-many relationships
      • Getting started
    • Time for action – mapping relationships
    • Fluent mapping
    • Summary
  • Chapter 5: The Session Procession
    • What is an NHibernate session?
    • Time for action – getting ready
    • What is a session factory?
    • Creating your first session
    • Why do we call .Commit()?
    • NHibernate session versus database session?
    • Time for action – creating a session and doing some CRUD
    • Sessions in ASP.NET
    • Summary
  • Chapter 6: I'm a Logger
    • Why do we need to log?
    • Why log4net?
    • Getting started
    • Configuration
    • Time for action – adding some logging
    • NHibernate log messages
    • Appenders
    • Creating a logger
    • Time for action – adding some custom logging
    • Summary
  • Chapter 7: Configuration
    • Looking back
    • The basics of configuration
    • Taking a look at the SQL
    • Abstracting the configuration
    • Time for action – moving our configuration
    • XML configuration
    • Summary
  • Chapter 8: Writing Queries
    • Using Data Access Objects
    • The basic Data Access Object
    • Time for action – creating our basic Data Access Object
    • Data Access Object methods
    • Time for action – adding some CRUD methods
    • Coding some GetX() methods
    • The FieldNames structure
    • Time for action – expanding our capabilities
    • The ICriteria object
    • Creating a GetAll() method
    • Paging and Sorting GetAll() methods
    • Filtering ICriteria
    • Time for action – replacing our inline code
    • Summary
  • Chapter 9: Binding Data
    • Why should we use data binding?
    • Time for action – adding an ASP.NET project
    • Basic data binding techniques
    • Common data binding methods
    • A simple templated control
    • Creating a control instance
    • The <asp:ListView> control
    • The <asp:ObjectDataSource> control
    • Time for action – adding our first data bound control
    • Direct data binding
    • One last control—the <asp:FormView>
    • Summary
  • Chapter 10: .NET Security
    • Built-in controls
    • Time for action – adding a login to our page
    • Membership providers
    • Location security
    • Configuring our provider
    • Time for action – create a membership provider
    • User roles
    • Role providers
    • Provider configuration
    • Summary
  • Chapter 11: It's a Generation Thing
    • Judging requirements
    • CodeSmith
    • nhib-gen
    • AjGenesis
    • Visual NHibernate
    • MyGeneration
    • Time for action – using MyGeneration
    • NGen NHibernate Code Generator
    • NHModeller
    • Microsoft T4 templates
    • T4 hbm2net
    • Summary
  • Chapter 12: Odds and Ends
    • Unit of Work and Burrow
    • How does this relate to NHibernate?
    • Blog.Net blogging components
    • maxRequestLength
    • Converting CSS templates
    • Time for action – converting a CSS template
    • XML documentation & GhostDoc
    • Summary
  • Appendix: Pop Quiz Answers
    • Chapter 2 – Database Layout and Design
      • Relationships
    • Chapter 3 – A Touch of Class
      • Mapping
    • Chapter 4 – Data Cartography
      • Class mapping
    • Chapter 5 – The Session Procession
      • Creating and updating records
    • Chapter 6 – I'm Logger
      • Logging
    • Chapter 7 – Configuration
      • Basic configuration
    • Chapter 8 – Writing Queries
      • Fieldnames and ICriteria
    • Chapter 9 – Binding Data
      • Basic data binding
    • Chapter 10 – .NET Security
      • Access configuration
    • Chapter 12 – Odds and Ends
      • Burrowing in

Aaron Cure

Aaron Cure is an avid developer, instructor, and innovator. During his 10 years in the military as a linguist and a satellite communications repair technician, he learned that his real love was computer programming. After various throes with PHP, Classic ASP, VB, and a brief encounter with Java/JSP, he found a real passion for the .NET framework. After searching for a "better way" to carry out database storage and retrieval, Aaron stumbled across the NHibernate framework. Unsatisfied with the options for interacting with this great framework, he founded the NHibernate Generation project (nhib-gen) on SourceForge to reduce the "barrier to entry" for most developers. Aaron and his family run a small consulting and web hosting company doing web design and custom software development for various organizations across the country. One of their more interesting projects has been software to control laser cutting machines. In his spare time, he also enjoys developing projects with his daughters, using everything from Lego NXT (using C# and Bluetooth communications) to the Microchip PIC platform (using JAL and USB). He also collects and restores classic farm tractors, engines, and farm equipment, as well as semi trucks and trailers. He and his family display them at tractor shows, parades, schools, and various other community events.

Code Downloads

Download the code and support files for this book.

Submit Errata

Please let us know if you have found any errors not listed on this list by completing our errata submission form. Our editors will check them and add them to this list. Thank you.


- 4 submitted: last submission 29 Apr 2014

Errata type: Code | Page number: 142

In Step 6, the return SessionFactory.OpenSession()Session; is not a valid syntax. It should be SessionFactory.OpenSession() as OpenSession returns an ISession, which is what is desired.


Errata type: Code | Page number: 88

cfg.AddAssembly(typeof(Address).AssemblyQualifiedName); Should be: cfg.AddAssembly(typeof(Address).Assembly);


Errata type: Code | Page number: 89

cfg.AddAssembly(GetType(Address).AssemblyQualifiedName) should be: cfg.AddAssembly(GetType(Address).Assembly)


Errata type:code Page No:25

The faulty method is private Type GetUnproxiedType() { return GetType(); } This method does not return an unproxied type. IMHO the method should look something like this: private Type GetUnproxiedType() { var proxy = this as INHibernateProxy; if (proxy != null) { return proxy.HibernateLazyInitializer.PersistentClass; } return GetType(); }

Sample chapters

You can view our sample chapters and prefaces of this title on PacktLib or download sample chapters in PDF format.

Frequently bought together

NHibernate 2 Beginner's Guide +    Joomla! 2.5 Beginner’s Guide =
50% Off
the second eBook
Price for both: £24.65

Buy both these recommended eBooks together and get 50% off the cheapest eBook.

What you will learn from this book

  • Use NHibernate to retrieve and store data in your database
  • Develop an efficient, robust data access layer with little or no code
  • Design or modify your database for high performance
  • Connect to multiple databases or database backends with simple code
  • Implement base classes to provide basic functionality for all database objects
  • Improve the performance of your data retrieval methods with proven open source technology
  • Make web applications faster using strong caching strategies such as memcached
  • Simplify data storage and display using built-in .NET data controls with NHibernate
  • Using NHibernate to control application security using Membership and Role providers and .NET security controls

In Detail

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.

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.

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.

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.

This easy-to-follow guide will show you how to connect the NHibernate object-relational mapper to your projects to create a rich, efficient, object-oriented data access layer with little or no additional work


This is a beginner's guide to NHibernate that starts from ground zero. Successive chapters build upon earlier concepts, while the sample code presents various ways to accomplish typical data access tasks. Within a few chapters you have a running application using NHibernate to retrieve and store data.

We examine all of the topics required to get a functional data access layer implemented by writing the least amount of code possible, presenting options along the way to handle particular edge cases or situations as they arise.

When you have completed the various exercises you will have running data-bound desktop and web applications, as well as an understanding of how to implement NHibernate in your own applications.

Who this book is for

This book is for new and seasoned developers of .NET web or desktop applications who want a better way to access database data. It is a basic introduction to NHibernate, with enough information to get a solid foundation in using NHibernate. Some advanced concepts are presented where appropriate to enhance functionality or in situations where they are commonly used.

Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software