Intoduction to NHibernate

17
Intoduction to NHibernate

description

Intoduction to NHibernate. Agenda. Overview of NHibernate Models and Mappings Configuration Sessions and Transactions Queries. What is Nhibernate ?. mature, ORM solution for .NET platform free, GNU Lesser General Public License - PowerPoint PPT Presentation

Transcript of Intoduction to NHibernate

Page 1: Intoduction to NHibernate

Intoduction to NHibernate

Page 2: Intoduction to NHibernate

Agenda

• Overview of NHibernate• Models and Mappings• Configuration• Sessions and Transactions• Queries

Page 3: Intoduction to NHibernate

What is Nhibernate ?

• mature, ORM solution for .NET platform• free, GNU Lesser General Public License• mapping an object-oriented domain model to a relational

database• home == nhforge.org• files on sourceforge• groups http://groups.google.com/group/nhusers• Commercial support

– hibernating rhinos - Ayende Rahien– imeta - Steve Strong, Fabio Maulo

Page 4: Intoduction to NHibernate

History• started as port of the popular Java O/R mapper

Hibernate to .NET• Hibernate was started in 2001 by Gavin King• NHibernate was started around 2003

– ver 1.0 mirrored the feature set of Hibernate 2.1– ver 1.2.1, released in 11/2007, features from Hibernate 3 and

support for .NET 2.0, stored procedures, generics, and nullable types

– ver 2.0 was released 08/2008. Comparable to Hibernate 3.2. .NET 1.1

– ver 3.0 - December 04, 2010 - .NET 3.5. • LINQ support, strongly typed criteria-like API called QueryOver, new AST-

based parser for NHibernate's HQL, ...

– http://sourceforge.net/projects/nhibernate/files/NHibernate/

Page 5: Intoduction to NHibernate

Mappings

• mapping a class with XML– Keys, ID generators

• table-per-class hierarchy• table per class• table per concrete class• one-to-many relationship

– lazy loading collections, lazy loading proxies

• setting up a base entity class• handling versioning and concurrency

Page 6: Intoduction to NHibernate

• bidirectional one-to-many class relationships• mappings enumerations• creating class components

Page 7: Intoduction to NHibernate

mapping a class with XML

• 2x .xsd – intellisense• mapping file - XML - extension .hbm.xml• set Build Action to Embedded Resource !!!• model - collection of classes that will be

persisted in the database• persistent class - any class that will be

persisted (e.g. Person, Address)• entity class - a persistent class with an ID

Page 8: Intoduction to NHibernate

• entity - an instance of an entity class• POCO - Plain Old CLR Object

– POCOs are objects not burdened with inheritance or attributes needed for specific frameworks

• all entity classes should be persistence ignorant

– strongly held design decisions in NHibernate

• Id property - primary key value from the db• persistent object identifier (POID)

Page 9: Intoduction to NHibernate

Persistence and the layered architecture

• business layer – is responsible for implementing any business rules or

system requirements that users would understand as part of the problem domain

• persistence layer – group of classes and components responsible for

saving application data to and retrieving it from data store

– defines a mapping between the business domain

entities and the database. – NH lives here !!!

Page 10: Intoduction to NHibernate

Approaches to begin developing an NH application

• model-first– create model -> map the model -> generate our

database tables from the model and mappings

• configuration-first • database-first

Page 11: Intoduction to NHibernate

Non-insert POID generators

• assign an identity to a persistent object without writing the object's data to the db

• hilo• guid• guid.comb• guid.native• uuid.hex, uuid.string• counter, increment• sequence, seqhilo

Page 12: Intoduction to NHibernate

Post-insert POID generators

• require data to be persisted to the database for an ID to be generated

• identity• select (uses natural id)• sequence-identity• trigger-identity• native

Page 13: Intoduction to NHibernate

table-per-class hierarchy

• data for our entire hierarchy is stored in a single table

• discriminator column ProductType– distinguish among products, books, and movies– by default, the contains the class name– Eg.Core.Product, Eg.Core.Book, or Eg.Core.Movie

• subclass properties as must be nullable• suggested method for mapping class hierarchies

Page 14: Intoduction to NHibernate

table-per-class

• properties of the base class in a shared table• each subclass gets its own table• joined-subclass element

– key element to name the primary key column

Page 15: Intoduction to NHibernate

table-per-concrete class

• each class gets its own table • containing columns for all properties of the class

and the base class• there is no duplication of data• union-subclass element

Page 16: Intoduction to NHibernate
Page 17: Intoduction to NHibernate