NHibernate Presentation

12
Nhibernate - Introduction NHibernate is a port of Hibernate Core for Java to the .NET Framework. It handles persisting plain .NET objects to and from an underlying relational database. Given an XML description of your entities and relationships, NHibernate automatically generates SQL for loading and storing the objects. Optionally, you can describe your mapping metadata with attributes in your source code. NHibernate supports transparent persistence, your object classes don't have to follow a restrictive programming model. Persistent classes do not need to implement any interface or inherit from a special base class. This makes it possible to design the business logic using plain .NET (CLR) objects and object-oriented idiom.

Transcript of NHibernate Presentation

Page 1: NHibernate Presentation

8/8/2019 NHibernate Presentation

http://slidepdf.com/reader/full/nhibernate-presentation 1/12

Nhibernate - Introduction

NHibernate is a port of Hibernate Core for Java to the .NET Framework. Ithandles persisting plain .NET objects to and from an underlying relationaldatabase.

Given an XML description of your entities and relationships, NHibernateautomatically generates SQL for loading and storing the objects. Optionally,you can describe your mapping metadata with attributes in your source code.

NHibernate supports transparent persistence, your object classes don't have tofollow a restrictive programming model. Persistent classes do not need toimplement any interface or inherit from a special base class. This makes itpossible to design the business logic using plain .NET (CLR) objects andobject-oriented idiom.

Page 2: NHibernate Presentation

8/8/2019 NHibernate Presentation

http://slidepdf.com/reader/full/nhibernate-presentation 2/12

Nhibernate - Features

●Natural programming model - NHibernate supports natural OO idiom; inheritance, polymorphism,composition and the .NET collections framework, including generic collections.

●Native .NET - NHibernate API uses .NET conventions and idioms.

Support for fine-grained object models - a rich variety of mappings for collections and dependentobjects.●No build-time bytecode enhancement - there's no extra code generation or bytecode processingsteps in your build procedure.

●The query options - NHibernate addresses both sides of the problem; not only how to get objectsinto the database, but also how to get them out again.

Custom SQL - specify the exact SQL that NHibernate should use to persist your objects. Storedprocedures are supported on Microsoft SQL Server.

●Support for "conversations" - NHibernate supports long-lived persistence contexts, detach/reattachof objects, and takes care of optimistic locking automatically.

●Free/open source - NHibernate is licensed under the LGPL (Lesser GNU Public License).

Page 3: NHibernate Presentation

8/8/2019 NHibernate Presentation

http://slidepdf.com/reader/full/nhibernate-presentation 3/12

Page 4: NHibernate Presentation

8/8/2019 NHibernate Presentation

http://slidepdf.com/reader/full/nhibernate-presentation 4/12

Page 5: NHibernate Presentation

8/8/2019 NHibernate Presentation

http://slidepdf.com/reader/full/nhibernate-presentation 5/12

Nhibernate – Installations and reference

Download and Install from:http://sourceforge.net/projects/nhibernate/files/

●Getting Started – following these stephttp://nhforge.org/wikis/howtonh/your-first-nhibernate-based-application.aspx

Page 6: NHibernate Presentation

8/8/2019 NHibernate Presentation

http://slidepdf.com/reader/full/nhibernate-presentation 6/12

Nhibernate – Database support

Database supported:●Microsoft SQL Server 2005/2000●Oracle●Microsoft Access●Firebird●PostgreSQL●DB2 UDB●MySQL●SQLite

Get more information from:●http://community.jboss.org/wiki/DatabasessupportedbyNHibernate

●http://community.jboss.org/wiki/UsingNHibernatewithVisualStudioNET

●http://community.jboss.org/wiki/UsingNHibernatewithASPNET

●http://nhforge.org/doc/nh/en/index.html

Page 7: NHibernate Presentation

8/8/2019 NHibernate Presentation

http://slidepdf.com/reader/full/nhibernate-presentation 7/12

Nhibernate – Configuration Section

<?xml version="1.0" ?><hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >

<session-factory><property name="connection.provider ">

Nhibernate.Connection.DriverConnectionProvider </property><property name="dialect">

Nhibernate.Dialect.MsSql2005Dialect</property>

<property name="connection.driver_class ">Nhibernate.Driver.SqlClientDriver 

</property><property name="connection.connection_string">

Server=(local);Initial Catalog=dbname;User Id=user;Password=********</property>

<property name="show_sql">true</property><property name="command_timeout">60</property>

</session-factory></hibernate-configuration>

Page 8: NHibernate Presentation

8/8/2019 NHibernate Presentation

http://slidepdf.com/reader/full/nhibernate-presentation 8/12

Nhibernate – cat.hbm.xml

<?xml version="1.0"?><hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Eg"  namespace="Eg">

<class name="Cat" table="CATS" discriminator-value="C"> <id name="Id" column="uid" type="Int64">

<generator class="hilo"/>

</id><discriminator column="subclass" type="Char"/><property name="BirthDate" type="Date"/><property name="Color" not-null="true"/><property name="Sex" not-null="true" update="false"/><property name="Weight"/>

 <many-to-one name="Mate" column="mate_id"/><set name="Kittens">

<key column="mother_id"/><one-to-many class="Cat"/>

</set><subclass name="DomesticCat" discriminator-value="D">

<property name="Name" type="String"/></subclass>

</class>

<class name="Dog"><!-- mapping for Dog could go here -->

</class>

</hibernate-mapping>

Page 9: NHibernate Presentation

8/8/2019 NHibernate Presentation

http://slidepdf.com/reader/full/nhibernate-presentation 9/12

Page 10: NHibernate Presentation

8/8/2019 NHibernate Presentation

http://slidepdf.com/reader/full/nhibernate-presentation 10/12

Nhibernate - Property

<propertyname="propertyName"             (1)

column="column_name"           (2)type="typename"                     (3)update="true|false"                 (4)insert="true|false"                  (4)formula="arbitrary SQL expression"   (5)access="field|property|ClassName"    (6)optimistic-lock="true|false"                     (7)

generated="never|insert|always"        (8)/>

Page 11: NHibernate Presentation

8/8/2019 NHibernate Presentation

http://slidepdf.com/reader/full/nhibernate-presentation 11/12

Nhibernate – Mapping - One-to-One

<one-to-onename="PropertyName"                                 class="ClassName"                                  cascade="all|none|save-update|delete"        (3)constrained="true|false"                                fetch="join|select"                                  

property-ref="PropertyNameFromAssociatedClass"     (6)access="field|property|nosetter|ClassName"           (7)

/>

<one-to-one name="Person" class="Person"/> – Person class<one-to-one name="Employee" class="Employee" constrained="true"/> – Employee

<class name="Person" table="PERSON"><id name="Id" column="PERSON_ID">

<generator class="foreign"><param name="property">Employee</param>

</generator></id>...<one-to-one name="Employee"  class="Employee" constrained="true"/>

</class>

Page 12: NHibernate Presentation

8/8/2019 NHibernate Presentation

http://slidepdf.com/reader/full/nhibernate-presentation 12/12

Nhibernate – Mapping- many-to-one

<many-to-one

name="PropertyName"                              (1)column="column_name"                            (2)class="ClassName"                                  cascade="all|none|save-update|delete"    (4)fetch="join|select"                                   update="true|false"                                   insert="true|false"                                   property-ref="PropertyNameFromAssociatedClass"     (7)access="field|property|nosetter|ClassName"                 (8)unique="true|false"                              optimistic-lock="true|false"                                                 (not-found="ignore|exception"                           

/>

<many-to-one name="product" class="Product" column="PRODUCT_ID"/>