![Page 1: Introduction to NHibernate By Andrew Smith. The Basics Object Relation Mapper Maps POCOs to database tables Based on Java Hibernate. V stable Generates](https://reader033.vdocument.in/reader033/viewer/2022061305/5513fe8d550346dd488b47c7/html5/thumbnails/1.jpg)
Introduction to NHibernate
By Andrew Smith
![Page 2: Introduction to NHibernate By Andrew Smith. The Basics Object Relation Mapper Maps POCOs to database tables Based on Java Hibernate. V stable Generates](https://reader033.vdocument.in/reader033/viewer/2022061305/5513fe8d550346dd488b47c7/html5/thumbnails/2.jpg)
The Basics
• Object Relation Mapper
• Maps POCOs to database tables
• Based on Java Hibernate. V stable
• Generates SQL at run time
• Database agnostic
![Page 3: Introduction to NHibernate By Andrew Smith. The Basics Object Relation Mapper Maps POCOs to database tables Based on Java Hibernate. V stable Generates](https://reader033.vdocument.in/reader033/viewer/2022061305/5513fe8d550346dd488b47c7/html5/thumbnails/3.jpg)
Entity definitions
• “Persistence ignorance”– No need for special base class– No need to implement special interfaces
• Default constructor• Identity property (Primary key)• To support lazy loading, public properties
and methods must be virtual• Collection properties must be declared as
an interface type
![Page 4: Introduction to NHibernate By Andrew Smith. The Basics Object Relation Mapper Maps POCOs to database tables Based on Java Hibernate. V stable Generates](https://reader033.vdocument.in/reader033/viewer/2022061305/5513fe8d550346dd488b47c7/html5/thumbnails/4.jpg)
Class Diagram
![Page 5: Introduction to NHibernate By Andrew Smith. The Basics Object Relation Mapper Maps POCOs to database tables Based on Java Hibernate. V stable Generates](https://reader033.vdocument.in/reader033/viewer/2022061305/5513fe8d550346dd488b47c7/html5/thumbnails/5.jpg)
Configuration
• Multiple options – Xml, ActiveRecord, Fluent NH
Xml:• 1 overall configuration section• Multiple “<entityname>.hbm.xml”
embedded resource files• Intellisense: Place .xsd files in “Microsoft
Visual Studio 9.0\Xml\Schemas”
![Page 6: Introduction to NHibernate By Andrew Smith. The Basics Object Relation Mapper Maps POCOs to database tables Based on Java Hibernate. V stable Generates](https://reader033.vdocument.in/reader033/viewer/2022061305/5513fe8d550346dd488b47c7/html5/thumbnails/6.jpg)
Sessions
• Very quick to create session
• Disposable (may hold a DB connection open)
• Web: session per request. (Also, ‘long conversations’ - check out NHibernate Burrow)
![Page 7: Introduction to NHibernate By Andrew Smith. The Basics Object Relation Mapper Maps POCOs to database tables Based on Java Hibernate. V stable Generates](https://reader033.vdocument.in/reader033/viewer/2022061305/5513fe8d550346dd488b47c7/html5/thumbnails/7.jpg)
Mappings: Primary keys
• Entities must be identifiable
• Multiple strategies for PK
• Use ‘native’ as best-guess
• Recommended way is “HiLo” to save round-tripping to DB
![Page 8: Introduction to NHibernate By Andrew Smith. The Basics Object Relation Mapper Maps POCOs to database tables Based on Java Hibernate. V stable Generates](https://reader033.vdocument.in/reader033/viewer/2022061305/5513fe8d550346dd488b47c7/html5/thumbnails/8.jpg)
Mappings: Properties
• Maps database columns to properties/fields on entity
• Can map columns to nested types
• Control insert/update per column
• Default is to insert/update all columns, but can be dynamic
![Page 9: Introduction to NHibernate By Andrew Smith. The Basics Object Relation Mapper Maps POCOs to database tables Based on Java Hibernate. V stable Generates](https://reader033.vdocument.in/reader033/viewer/2022061305/5513fe8d550346dd488b47c7/html5/thumbnails/9.jpg)
Demo: The basics
• Fetching single entity by ID
• Updating
• Inserting
![Page 10: Introduction to NHibernate By Andrew Smith. The Basics Object Relation Mapper Maps POCOs to database tables Based on Java Hibernate. V stable Generates](https://reader033.vdocument.in/reader033/viewer/2022061305/5513fe8d550346dd488b47c7/html5/thumbnails/10.jpg)
Mappings: Relations
• Many types. Most common: bag and set
• Assuming bi-directional relations and simple bag collections
• In a bi-directional association, only one end can be the ‘owner’
• Identify the ‘non-owner’ end of collection by setting Inverse=“true”
![Page 11: Introduction to NHibernate By Andrew Smith. The Basics Object Relation Mapper Maps POCOs to database tables Based on Java Hibernate. V stable Generates](https://reader033.vdocument.in/reader033/viewer/2022061305/5513fe8d550346dd488b47c7/html5/thumbnails/11.jpg)
Mappings: 1-* Relations
• Professor (1) – Class (*)
• Bi-directional: <bag> and <many-to-one>
• Declare the many-valued end inverse="true“
• Use ‘not-found=ignore’ for bad, legacy data
![Page 12: Introduction to NHibernate By Andrew Smith. The Basics Object Relation Mapper Maps POCOs to database tables Based on Java Hibernate. V stable Generates](https://reader033.vdocument.in/reader033/viewer/2022061305/5513fe8d550346dd488b47c7/html5/thumbnails/12.jpg)
Mappings: *-* Relations
• Student (*) – Class (*)
• Bi-directional: 2 <bag> elements with link table name
• Arbitrarily choose 1 end to set as Inverse=“true”
• Demo
![Page 13: Introduction to NHibernate By Andrew Smith. The Basics Object Relation Mapper Maps POCOs to database tables Based on Java Hibernate. V stable Generates](https://reader033.vdocument.in/reader033/viewer/2022061305/5513fe8d550346dd488b47c7/html5/thumbnails/13.jpg)
Cascades
• Don’t have to explicitly call save / update / delete on related entities
• Cascade types: none, save-update, delete, all, all-delete-orphan
• Can define a default cascade
• Q’s?
![Page 14: Introduction to NHibernate By Andrew Smith. The Basics Object Relation Mapper Maps POCOs to database tables Based on Java Hibernate. V stable Generates](https://reader033.vdocument.in/reader033/viewer/2022061305/5513fe8d550346dd488b47c7/html5/thumbnails/14.jpg)
Proxies
• Enables lazy loading - Just in time loading of data– Lazy loaded collections– Lazy loaded entities (the single-end of a
relation)– Controlled via mappings or code
• Watch for N+1 issue
![Page 15: Introduction to NHibernate By Andrew Smith. The Basics Object Relation Mapper Maps POCOs to database tables Based on Java Hibernate. V stable Generates](https://reader033.vdocument.in/reader033/viewer/2022061305/5513fe8d550346dd488b47c7/html5/thumbnails/15.jpg)
Querying
• Lots of options:– Criteria– DetachedCriteria– HQL– Linq– SQL
![Page 16: Introduction to NHibernate By Andrew Smith. The Basics Object Relation Mapper Maps POCOs to database tables Based on Java Hibernate. V stable Generates](https://reader033.vdocument.in/reader033/viewer/2022061305/5513fe8d550346dd488b47c7/html5/thumbnails/16.jpg)
Querying: ICriteria
• Out-of-the-box method for building up a query in code
• Requires an active session
• Weakly typed
• nhlambdaextensions project adds typesafe lambda expressions
![Page 17: Introduction to NHibernate By Andrew Smith. The Basics Object Relation Mapper Maps POCOs to database tables Based on Java Hibernate. V stable Generates](https://reader033.vdocument.in/reader033/viewer/2022061305/5513fe8d550346dd488b47c7/html5/thumbnails/17.jpg)
Querying: Projections
• By default NH selects all mapped properties of entity
• Projections allow control over the ‘select’ part of generated SQL
![Page 18: Introduction to NHibernate By Andrew Smith. The Basics Object Relation Mapper Maps POCOs to database tables Based on Java Hibernate. V stable Generates](https://reader033.vdocument.in/reader033/viewer/2022061305/5513fe8d550346dd488b47c7/html5/thumbnails/18.jpg)
Querying: Aliases / Subcriteria
• Alias relations to refer to them later
• Need to use them to reference multi-level relations. E.g. OrderItem.OrderHeader.Customer.Name
![Page 19: Introduction to NHibernate By Andrew Smith. The Basics Object Relation Mapper Maps POCOs to database tables Based on Java Hibernate. V stable Generates](https://reader033.vdocument.in/reader033/viewer/2022061305/5513fe8d550346dd488b47c7/html5/thumbnails/19.jpg)
Querying: DetachedCriteria
• Same capabilities as ICriteria
• Does not require active session
• Can be ‘attached’ to any session at any time to execute query
![Page 20: Introduction to NHibernate By Andrew Smith. The Basics Object Relation Mapper Maps POCOs to database tables Based on Java Hibernate. V stable Generates](https://reader033.vdocument.in/reader033/viewer/2022061305/5513fe8d550346dd488b47c7/html5/thumbnails/20.jpg)
Querying: MultiCriteria
• Enables multiple criteria to be evaluated in the one round trip to DB
• Very useful for paging
![Page 21: Introduction to NHibernate By Andrew Smith. The Basics Object Relation Mapper Maps POCOs to database tables Based on Java Hibernate. V stable Generates](https://reader033.vdocument.in/reader033/viewer/2022061305/5513fe8d550346dd488b47c7/html5/thumbnails/21.jpg)
Querying: HQL
• Similar to SQL
• Allows querying over domain entities
• “HQL is fully object-oriented, understanding notions like inheritance, polymorphism and associations”
![Page 22: Introduction to NHibernate By Andrew Smith. The Basics Object Relation Mapper Maps POCOs to database tables Based on Java Hibernate. V stable Generates](https://reader033.vdocument.in/reader033/viewer/2022061305/5513fe8d550346dd488b47c7/html5/thumbnails/22.jpg)
Querying: Linq• NHibernate Linq 1.0 released• Supports just about anything you can do with criteria API
![Page 23: Introduction to NHibernate By Andrew Smith. The Basics Object Relation Mapper Maps POCOs to database tables Based on Java Hibernate. V stable Generates](https://reader033.vdocument.in/reader033/viewer/2022061305/5513fe8d550346dd488b47c7/html5/thumbnails/23.jpg)
Caching: 1st level cache
• Every ISession has an in-built cache called 1st level cache
• Stores all the loaded entities for current unit of work
• Prevents needless round-tripping to DB
![Page 24: Introduction to NHibernate By Andrew Smith. The Basics Object Relation Mapper Maps POCOs to database tables Based on Java Hibernate. V stable Generates](https://reader033.vdocument.in/reader033/viewer/2022061305/5513fe8d550346dd488b47c7/html5/thumbnails/24.jpg)
Caching: 2nd level cache
• Cache shared between sessions• Out of the box support for:
– NHibernate.Caches.Prevalence – NHibernate.Caches.SysCache (ASP.Net cache)– NHibernate.Caches.SysCache2 (SQL
dependency-based expiration)– NHibernate.Caches.MemCache
• Does all the hard work for you!
![Page 25: Introduction to NHibernate By Andrew Smith. The Basics Object Relation Mapper Maps POCOs to database tables Based on Java Hibernate. V stable Generates](https://reader033.vdocument.in/reader033/viewer/2022061305/5513fe8d550346dd488b47c7/html5/thumbnails/25.jpg)
Querying: Future Queries
• Executes a batch of queries in one round trip to DB
![Page 26: Introduction to NHibernate By Andrew Smith. The Basics Object Relation Mapper Maps POCOs to database tables Based on Java Hibernate. V stable Generates](https://reader033.vdocument.in/reader033/viewer/2022061305/5513fe8d550346dd488b47c7/html5/thumbnails/26.jpg)
Why use it?• Reduces repetitive, error prone data access code• Enables use of OOP• Very flexible• Stable, widely used• Speed up development time
• Single place to add behaviour such as auditing / INotifyPropertyChanged / Filters etc
• Testing: Easier integration tests• Security: Uses parameterised queries by default
![Page 27: Introduction to NHibernate By Andrew Smith. The Basics Object Relation Mapper Maps POCOs to database tables Based on Java Hibernate. V stable Generates](https://reader033.vdocument.in/reader033/viewer/2022061305/5513fe8d550346dd488b47c7/html5/thumbnails/27.jpg)
Resources
• Book: NHibernate In Action (v 1.2)
• NHForge.org– NHibernate blog
• Nhibernate FAQ blog
• Very active users group (nhusers)
• ayende.com
• Dimecast and “Summer of NHibernate” videos