Intoduction to NHibernate. Agenda Overview of NHibernate Models and Mappings Configuration Sessions and Transactions Queries.

Slides:



Advertisements
Similar presentations
Introduction to NHibernate By Andrew Smith. The Basics Object Relation Mapper Maps POCOs to database tables Based on Java Hibernate. V stable Generates.
Advertisements

An Object/Relational Mapping tool Free and open source Simplifies storage of object data in a relational database Removes the need to write and maintain.
WaveMaker Visual AJAX Studio 4.0 Training
Michael Pizzo Software Architect Data Programmability Microsoft Corporation.
.NET Database Technologies: Open-Source Frameworks.
DEVELOPING APPLICATIONS WITH LINQ 2 SQL Sidar Ok
Hibernate 1. Introduction ORM goal: Take advantage of the things SQL databases do well, without leaving the Java language of objects and classes. ORM.
Object Oriented System Development with VB .NET
Chapter 12 Database Connectivity with ASP.NET JavaScript, Third Edition.
ORM Technologies and Entity Framework (EF)
Session-01. Hibernate Framework ? Why we use Hibernate ?
Data Persistence and Object-Relational Mapping Slides by James Brucker, used with his permission 1.
CSE446 S OFTWARE Q UALITY M ANAGEMENT Spring 2014 Yazılım ve Uyguluma Geliştirme Yöneticisi Orhan Başar Evren.
Enterprise Object Framework. What is EOF? Enterprise Objects Framework is a set of tools and resources that help you create applications that work with.
Object and object-relational databases 1. Object databases vs. Object-relational databases Object databases Stores complex objects – Data + functions.
Neo.NET Entity Objects Design Goals Copyright © Erik Dörnenburg – Last updated: May 2004.
1 Entity Framework Introduction. Outline Goals of Entity Framework 2.
.NET Database Technologies: Entity Framework additional notes – part 2.
Data Access Patterns Some of the problems with data access from OO programs: 1.Data source and OO program use different data modelling concepts 2.Decoupling.
1 Overview of Databases. 2 Content Databases Example: Access Structure Query language (SQL)
Eric Nelson (or )
NHibernate in Action Web Seminar at UMLChina By Pierre Henri Kuaté 2008/08/27
CHAPTER 14 USING RELATIONAL DATABASES TO PROVIDE OBJECT PERSISTENCE (ONLINE) © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database.
Computer Science 101 Database Concepts. Database Collection of related data Models real world “universe” Reflects changes Specific purposes and audience.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
.NET Database Technologies: Data Models and Patterns.
Ronnie Saurenmann Principal Architect Microsoft Switzerland.
Object Oriented Analysis and Design 1 Chapter 7 Database Design  UML Specification for Data Modeling  The Relational Data Model and Object Model  Persistence.
© 2011 Pearson Education, Inc. Publishing as Prentice Hall 1 Chapter 14 Using Relational Databases to Provide Object Persistence (Overview) Modern Database.
Hibernate Persistence. What is Persistence Persist data to database or other storage.  In OO world, persistence means persist object to external storage.
Intoduction to NHibernate. Agenda Overview of NHibernate Models and Mappings Configuration Sessions and Transactions Queries.
Hibernate 3.0. What is Hibernate Hibernate is a free, open source Java package that makes it easy to work with relational databases. Hibernate makes it.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Oct * Brad Tutterow. VS 2008.NET 3.5LINQ Entity Framework  The ADO.NET Entity Framework is part of Microsoft’s next generation of.NET technologies.
Intoduction to NHibernate. Agenda Overview of NHibernate Models and Mappings Configuration Sessions and Transactions Queries.
Fall CIS 764 Database Systems Engineering L18.2 : Object Relational Mapping … ….Object persistence.
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool The problem fixed by ORM Advantage Hibernate Hibernate Basic –Hibernate sessionFactory –Hibernate Session.
All information's of PLINQO in this Document, I got it from: So, you could visit the link above to research.
Hibernate Basics. Basics What is persistence? Persistence in Object Oriented Applications? Paradigm Mismatch.  The Problem of Granularity.  The Problem.
Database Connectivity with ASP.NET. 2 Introduction Web pages commonly used to: –Gather information stored on a Web server database Most server-side scripting.
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool Used in data layer of applications Implements JPA.
Entity Framework 7: What’s New? Ricardo Peres Technical Evangelist at Simplifydigital. Microsoft
CSCI 3327 Visual Basic Chapter 13: Databases and LINQ UTPA – Fall 2011.
Hibernate Thuy, Le Huu. Pentalog VN. Agenda Hibernate Annotations Improving performance – Lazy loading – Fetching Strategies – Dynamic insert, dynamic.
Sean Chambers.  ORM stands for Object Relational Mapper  Maps your POCO (plain old clr objects) to your relational model using XML config  Relieves.
 Who I am  What is NHibernate? ◦ Object Relational Mapper ◦ A framework for building advanced frameworks ◦ Ported from Java ◦ A wide NHibernate ecosystem.
CS422 Principles of Database Systems Object-Relational Mapping (ORM) Chengyu Sun California State University, Los Angeles.
CS520 Web Programming Object-Relational Mapping with Hibernate and JPA (I) Chengyu Sun California State University, Los Angeles.
uses of DB systems DB environment DB structure Codd’s rules current common RDBMs implementations.
Hibernate Java Persistence API. What is Persistence Persistence: The continued or prolonged existence of something. Most Applications Achieve Persistence.
Top 10 Entity Framework Features Every Developer Should Know
Introduction to Entity framework
Don't Know Jack About Object-Relational Mapping?
Chengyu Sun California State University, Los Angeles
Introduction to Entity Framework
EF Relations Object Composition
CS320 Web and Internet Programming SQL and MySQL
Entity Framework DB From Code, OOP Introduction
A very brief introduction
POOL persistency framework for LHC
Entity Framework By: Casey Griffin.
Learn. Imagine. Build. .NET Conf
ADO.NET Entity Framework
ADO.NEXT Advances in Data Access for 2008
Entity Framework Core (EF Core)
Chapter 8 Advanced SQL.
CS3220 Web and Internet Programming SQL and MySQL
Entity Framework Core for Enterprise Applications
CS3220 Web and Internet Programming SQL and MySQL
Chengyu Sun California State University, Los Angeles
Presentation transcript:

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 mapping an object-oriented domain model to a relational database home == nhforge.orgnhforge.org files on sourceforgesourceforge groups Commercial support –hibernating rhinos - Ayende Rahienhibernating rhinos - Ayende Rahien –imeta - Steve Strong, Fabio Mauloimeta - Steve Strong, Fabio Maulo

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 December 04, NET 3.5. LINQ support, strongly typed criteria-like API called QueryOver, new AST- based parser for NHibernate's HQL,... –

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

bidirectional one-to-many class relationships mappings enumerations

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

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)

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

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

Post-insert POID generators require data to be persisted to the database for an ID to be generated - strongly discouraged identity select (uses natural id) sequence-identity trigger-identity native

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

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

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

one-to-many relationship relate one entity to another

Lazy loading collections data isn't loaded from the database until it is required by the application 1.fetch Movie (Id, Name, Description, UnitPrice, and Director, but no Actors) 2.creates an instance of the Movie object 3.sets the properties of the Movie object with the data from the db 4.creates a special lazy loading object that implements IList, and sets the Actors property 5.returns Movie

foreach (var actor in movie.Actors) Console.WriteLine(actor.Actor); lazy loading object is initialized loads the associated ActorRole data disable lazy loading by adding the attribute lazy="false" to the list element

Lazy loading proxies supports lazy loading through the use of proxy objects public class ActorRole : Entity { public virtual string Actor { get; set; } public virtual string Role { get; set; } public virtual Movie Movie { get; set; } } proxy object is a subclass of Movie – requirements Movie cannot be a sealed class Movie must have a protected or public constructor without parameters All public members of Movie must be virtual. This includes methods

choices for the creation of these proxy objects –traditional choice DynamicProxy, part of the Castle –LinFu –Spring.NET –build your own lazy="false" on the class element of our Movie mapping to disable

Collections DuplicatesOrderType BagYesNoIList SetNo Iesi.Collections.ISet ListYes IList Map (Dictionary)Keys unique, Values noNoIDictionary most common types all collections may also use the ICollection type custom NHibernate.UserType.IuserCollection

Setting up a base entity class NH relies on the Equals() to determine equality application should not be aware of proxy objects Product instance with an ID of 8 should be equal to a different Product instance or Product proxy with an ID of 8 transient object is an object that has not been persisted to the db

Versioning and concurrency optimistic and pessimistic concurrency version element UPDATE Product SET Version = 2 */, Name = 'Junk' */, Description = 'Cool' */, UnitPrice = 100 */ WHERE Id = '764de11e-1fd0-491e db8015f9be5' */ AND Version = 1 */ If no rows updated StaleStateException the entity in memory is stale, or out of sync with the db

DateTime-based version fields –Datetime, DateTime2, timestamp attribute optimistic-lock –

Other mappings options Fluent Nhibernate public ProductMapping() { Id(p => p.Id).GeneratedBy.GuidComb(); DiscriminateSubClassesOnColumn("ProductType"); Version(p => p.Version); NaturalId().Not.ReadOnly().Property(p => p.Name); Map(p => p.Description); Map(p => p.UnitPrice).Not.Nullable(); }

ConfORM –convention-based mappings –mapper outputs an HbmMapping object built from our conventions –ConfORM uses conventions and patterns to build a mapping directly from the model –Customizable

Bidirectional one-to-many class relationships ORMs are designed to overcome the impedance mismatch between the object model and the relational model inverse attribute - determine which end of the relationship controls the foreign key we must keep both sides of relationship in sync

Mappings enumerations improperly mapped enumeration can lead to unnecessary updates by default, NH will map an enumeration to an int –AcctType = AccountTypes.Corporate –AcctType database field = 2 –int doesn't describe the business meaning store the name of the enumeration value –AcctType database field „Corporate“ type attribute –we tell NHibernate to use a custom class for conversion between.NET types and the database

Configuring NHibernate with App.config proxyfactory.factory_class –specifies the proxy framework we'll use dialect –specifies a dialect class that NHibernate uses to build SQL syntax connection.connection_string_name –references our connection string adonet.batch_size –group SQL DML statements in a single ADO.NET command mapping - where NHwill search for our mappings

components of NH app session represents a unit of work –NH session tracks changes to entities and writes those changes back to the database all at once –transactional write-behind

Dialects and drivers Microsoft SQL Server –CE, 7, 2000, 2005, 2008 Oracle –Lite, 8i, 9i, 10g MySql –MySql, MySql5 PostgreSQL, DB2, Informix, Sybase, Firebird, SQLite, Ingres

Configuring NHibernate with hibernate.cfg.xml separate xml configuration file