You are on page 1of 23

Cc thnh vin trong nhm tm hiu:

Nguyn Nht Cng


Cao Chuyn
Mohamed Hary
Abuljamin
Nguyn Quc Vit








BO CO KT QU TM HIU
NHIBERNATE
YOUR WELCOME
1.NHibernate v kin trc NHibernate

+NHibernate l mt cng c ORM(Object/Relation Mapping ) cho mi trng .NET .
ORM l mt k thut dng nh x t m hnh i tng sang m hnh quan h.

+ Hibernate l mt dch v lu tr v truy vn d liu quan h mnh m v nhanh. Hibernate
gip bn pht trin cc class dng lu tr d liu theo cch thc rt l hng i tng:
association, inheritance, polymorphism, composition v collections. Hibernate cho php bn thc
hin cc cu truy vn d liu bng cch s dng ngn ng SQL m rng ca Hibernate (HQL)
hoc l ngn ng SQL nguyn thu cng nh l s dng cc API.

+ NHibernate cn c kh nng truy vn , cp nht c s d liu , c th l chng ta c th
thc hin vic thm , xa , sa d liu v thc hin cc cu lnh select thng qua
NHibernate.

+Khng ging nh cc persistence layer khc, Hibernate khng n i sc mnh ca ngn ng
SQL khi bn m Hibernate cn m bo cho bn vic bn u t vo cng ngh v tri thc c
s d liu quan h l lun lun chnh xc. V iu quan trng hn na l Hibernate c license
theo LGPL (Lesser GNU Public License). Theo , bn c th thoi mi s dng Hibernate trong
cc d n open source hoc cc d n thng mi (commercial).

+NHibernate l mt cng c mapping object/relational cho mi trng .NET. Thut ng
mapping object/relational (ORM) lin quan ti k thut mapping mt m phng d liu ca mt
object model ti mt relational data model vi mt m hnh da trn SQL.

+Cc thnh phn ca Hibernate project:

* Hibernate Core: Cung cp cc chc nng c bn ca mt persistence layer cho cc ng dng
Java vi cc APIs v h tr XML mapping metadata.
* Hibernate Annotations: Cc map class vi JDK 5.0 annotations, bao gm Hibernate Validator.
* Hibernate EntityManager: S dng EJB 3.0 EntityManager API trong JSE hoc vi bt k JEE
server no.
* Hibernate Tools: Cc tool tch hp vi Eclipse v Ant dng cho vic sinh ra cc persistence
object t mt schema c sn trong database (reverse-engineering) v t cc file hbm sinh ra cc
class java hin thc cc persistence object, ri Hibernate t to cc object trong database
(forward-engineering).
* NHibernate: Hibernate cho .Net framework.
* JBoss Seam: Mt Java EE 5.0 framework cho pht trin cc ng dng JSF, Ajax v EJB 3.0
vi s h tr ca Hibernate. Seam hin rt mi v t ra rt mnh pht trin cc ng dng Web
2.0. N tch hp y tt c cc cng ngh "hot" nht hin nay.

Logo ca NHibernate :

i. Cc phin bn
NHibernate c bt u bi Tom Barrett. Cui nm 2005, JBoss (nay l 1 phn
ca Red Hat) v pht trin cc phin bn ca NHibernate. Cui nm 2006 th JBoss
ngng h tr d n ny v by gi NHibernate hon ton c pht trin bi cng
ng
Version 1.0 phn nh cc c im ca Hibernate 2.1 v 1 s c im
ca Hibernate 3
NHibernate 1.2.1 pht hnh vo thng 11 nm 2007, gii thiu nhiu c
im ca Hibernate 3 v h tr .NET 2.0 nh stored procedures, generics
v nhng kiu d liu c php null (nullable types)
NHibernate 2.0 pht hnh vo ngy 23 thng 8 nm 2008, n c th so
snh vi Hibernate 3.2 v cc tnh nng, v phin bn ny NHibernate
s khng h tr .NET 1.1
NHibernate 2.1 c pht hnh ngy 17 thng 7 nm 2009
NHibernate 3.0 c pht hnh ngy 4 thng 12 nm 2010 v l phin
bn u tin dng .NET 3.5. H tr tch hp LINQ v API mnh nh
QueryOver.
NHibernate 3.2 c pht hnh gn y vo khong thng 7 nm 2011
ii. Nhng im mi trong NHibernate 3.2
Mapping bng code : fluent configuration, khng cn nhng file .hbm.xml
Subselect: c th map SQL views nh cc entity
HQL paging: TAKE v SKIP trong HQL
Integrated bytecode provider: mt t DLL trin khai
iii. Ti sao dng NHibernate
ORM (Object Relational Mapping) l g
L 1 k thut chuyn i d liu gia cc h thng kiu d liu khng tng
thch trong ngn ng lp trnh hng i tng.
So snh vi k thut truyn thng
So vi k thut truyn thng l trao i gia 1 ngn ng hng i tng v 1
csdl quan h, ORM gim thiu s lng code bn phi vit 1 cch ng k.
Nhc im ca cc cng c O/R mapping : Trong cc lnh vc c quyn hay
k thut csdl c trng c ti u ha cao th hu ht cc cng c O/R mapping
khng thc hin tt vic xa b s lng ln d liu nh l stored procedures
u im : portable
iv. Bt u NHibernate u
NHibernate l 1 d n m ngun m.
Trang ch : http://www.nhforge.org
Code v th vin lu ti SourceForge :
http://sourceforge.net/projects/nhibernate/
v. Hng tip cn
C 3 hng tip cn :
To model, to map model, cu hnh NHibernate v cui cng to bng (table)
trong database t model v mappings
Cu hnh trc, sau thm mi entity v mapping tng ng cng lc, bng
(table) trong database c to t model v mappings
CSDL c sn ca ng dng khc, dng cc k thut mapping nng cao
vi. Cc thnh phn c bn
ISession : Interface ny tn t chi ph to v hy. ISession v c bn ch l
mt unit of work (n v lm vic)
ISessionFactory : Interface ny kh tn chi ph to v ng dng ca bn ch
nn to mt instance duy nht cho mi database m bn mun truy xut
IConfiguration : Theo nh ci tn ni ln c bn cht, interface
IConfiguration cu hnh cho NHibernate v c s dng trc khi to ra
ISessionFactory
ITransaction : Interface ny c s dng iu khin cc tng tc vi
database theo transactions. ITransaction ch h tr ch khng bt buc s
dng
IQuery : IQuery tn t chi ph v phi c s dng trong mt session to ra n
ICriteria : Cho php bn truy vn trn cc i tng theo cc iu kin no

Mapping 1 Model xung database
i. Mapping l g
Trc tin, ta cn bit database schema l m t cu trc ca database, m t
d liu c t chc trong database v c chia vo cc bng nh th no. N cng
ch r loi kiu d liu c th c lu tr, mi quan h gia cc bng vi nhau. Trong
c s d liu quan h, 1 schema nh ngha cc bng, cc trng (cc ct), cc mi
quan h, cc ch mc (indexes), cc rng buc, views, cc stored procedures.
Qua ta thy model l hng i tng, trong khi database schema l biu th
quan h.
Mapping nh ngha cch nh x 1 lp i tng xung 1 bng trong csdl, gm
cc thuc tnh, cc kiu d liu v cc rng buc, cc tham chiu ..Mapping nh ngha
lp no map vi bng no v thuc tnh no ca lp map vi ct no ca bng
ii. Cc loi mapping
XML-based mapping
Mi entity ca model c map vi 1 file XML document xung 1 bng trong
csdl. Cu trc ca XML document c nh ngha trong nhibernate-mapping.xsd file,
c download trong NHibernate
Cch mapping ny l cch truyn thng, rt linh hot
nh ngha 1 mapping, chng ta add 1 XML file vo d n vi ui m rng l
*.hbm.xml. File XML phi l 1 embeded resource ca d n, v ni dung ca n phi
theo cu trc c nh ngha trong .xsd file ni trn.
Attribute-based mapping
Thay v map 1 entity trong 1 file document ring bit xung 1 bng trong csdl, th
c th dng cc attribute lm vic
Fluent mapping
Vic mapping cc entity xung cc bng trong csdl 1 cch an ton dng fluent
API, n lm code d c v ngn gn
Vic mapping c nh ngha 1 lp (class) ring bit. Fluent NHibernate
framework download ti (http://www.fluentnhibernate.org)
Convention-based mapping, cn gi l mapping t ng
iii. XML Mapping
To 1 solution folder tn l Schema v add 2 file nhibernate-configuration.xsd v
nhibernate-mapping.xsd trong Nhibernate download vo folder Schema, nh hnh
sau :


nh ngha 1 mapping cho 1 entity, chng ta add mi 1 file XML vo d n.
File ny c ui m rng l .hbm.xml, v nn t tn ging tn ca entity. V d
Product.hbm.xml mapping Product entity.


Chng ta cng set thuc tnh Build Action ca XML file thnh Embeded
Resource nh hnh sau :


Ging nh bt k file XML document no khc, file XML mapping cng bt u
bng 1 khai bo XML, n gin l 1 th xml vi 2 thuc tnh version v encoding :
<?xml version=1.0 encoding=utf-8 ?>
Tip theo l th hibernate-mapping. Th ny c thuc tnh xmlns, namespace v
assembly ch ra ni m cha lp ta ang map
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="OrderingSystem.Domain"
assembly="OrderingSystem">
</hibernate-mapping>

1. Classes
Th tip theo cn nh ngha l th class, th ny bo cho NHibernate 2 th - 1
l ch ra lp m mapping document ny i din v 2 l ci bng trong csdl m lp
map ti.
Th lp c 2 thuc tnh (attributes) ta cn quan tm l name v table
<class name= table=>
</class>

2. Properties
Chng ta c th map cc thuc tnh (properties) ca lp vo cc trng trong
csdl dng th id v th property.
<property name="Name" type="String">
<column name="Name" length="255" sql-type="varchar" not-null="true"/>
</property>

Mt trong nhng thuc tnh m rng thng dng trong th id v property l
thuc tnh type. Bn c th ch cho NHibernate l bn dng kiu d liu no lu
thng tin ca lp, v d
<property name=Name type=String />

Nu bn khng dng thuc tnh type th NHibernate s t xc nh kiu d liu
theo phn x
3. ID columns
Thuc tnh u tin sau th class l bn mun map 1 thuc tnh Id. Th ny c
nhiu option bn la chn, v d n gin nht l :
<id name="Id">
<generator class="hilo"/>
</id>

Nhng dng ny ch cho NHibernate bit bn c 1 thuc tnh Id trong lp ca
bn, n c map xung 1 trng Id trong csdl. V y bn dng b to hilo cung
cp bi Nhibernate sinh s t tng cho trng Id.
Bn cng c th ch ra kiu d liu qua thuc tnh type.
<id name="Id" type=Int32>
<generator class="hilo"/>
</id>

Ngoi ra nu bn mun i tn ct trong csdl th lm nh sau
<id name="Id">
<column name="product_id"/>
<generator class="hilo"/>
</id>

Cc thuc tnh ph :
name : nh ngha tn ca ct trong csdl
length : chiu di ca trng trong csdl
sql-type : kiu d liu ca ct trong csdl
not-null : rng buc ct d liu c php null hay khng. Nu not-null=true th
khng c null

4. One-to-many relations
Gi s ta c entity l Order c thuc tnh(property) l LineItems, l tp hp ca
cc LineItem entity. Mi LineItem entity c 1 property l Order tham chiu n order
cha n. By gi ta map bn pha one ca mi quan h, tc l bn LineItem entity:
<many-to-one name="Order" class="Order" column="OrderId" not-null="true"/>
Trong th trn, thuc tnh name nh ngha tn ca property trong lp LineItem,
thuc tnh class nh ngha lp m property tham chiu, thuc tnh column ch ra tn
trng trong csdl, cha kha ngoi n bng Order. Cui cng not-null nh ngha l
kha ngoi OrderId khng c rng.
By gi ta map bn pha many. Trong Order mapping file, ta cn to 1 thnh
phn bag nm gi ton b lineItems. Mt bag l cch Nhibernate lu tr 1 tp hp k
th t cho php trng lp cc item. Ta c 1 thnh phn name nh ngha tn ca
property, thnh phn con key ch ra ct trong csdl m trng ny i din
<bag name="LineItems" inverse="true" cascade="all-delete-orphan">
<key column="OrderId"/>
<one-to-many class="LineItem"/>
</bag>
Bn s thy th one-to-many tng t nh th many-to-one ta to bn pha
one, bn bo Nhibernate v mi quan h inverse qua vic dng th inverse trong
thnh phn bag. Tc l bn chuyn quyn s hu mi quan h (relationship owner)
cho Order.
Th cascade ch cho NHibernate bit x l cc i tng nh th no khi chng
ta delete n.
Th lazy ch cho NHibernate dng lazy loading ngha l dng (record) s khng
c ko t database hay load vo b nh cho ti khi bn dng n, mc nh lazy
loading c m, bn mun tt th dng lazy =false.

5. Many-to-many relations
Quan h nhiu nhiu (many-to-many) c dng khc ph bin.

Trong hnh trn, bng BookAuthor dng kt 2 bng Book v Author.
NHibernate thng minh qun l nhng mi quan h nhiu nhiu
Ta biu din Authors trong lp Book vi 1 tp hp cc i tng Author nh sau
public IEnumerable<Author> Authors { get; set; }
Ta cng biu din Books trong lp Author vi 1 tp hp cc i tng Book nh
sau
public IEnumerable<Book> Books { get; set; }

Mapping mi quan h nhiu nhiu phc tp hn quan h 1 nhiu. Chng ta dng
1 th bag v c 1 th key bn trong. Chng ta cn thm thuc tnh table trong th bag
cho NHibernate bit bng no lu tr d liu quan h bn trong. Thay v dng thuc
tnh one-to-many v many-to-one, c 2 pha ta dng thnh phn many-to-many
Bn Book mapping file :
<bag name="Authors" table="BookAuthor">
<key column="BookId"/>
<many-to-many class="Author" column="AuthorId" />
</bag>
Bn Author mapping file :
<bag name="Books" table="BookAuthor">
<key column="AuthorId"/>
<many-to-many class="Book" column="BookId" />
</bag>

6. Mapping value objects
map cc thuc tnh ca value object, chng ta dng th component, v d
chng ta s map thuc tnh name ca entity Customer nh sau :
<component name="Name" class="Name">
<property name="LastName" length="50" not-null="true"/>
<property name="FirstName" length="50" not-null="true"/>
<property name="MiddleName" length="50"/>
</component>

7. Hng dn tng bc mapping vi XML
To mi 1 project, chn Console Application, t tn l
XmlMappingSample.
Add reference n assembly NHibernate.dll vo d n
Trong ca s Solution Explorer, click phi v solution, chn Add|New
Folder, t tn l Schema
Click phi Schema folder, chn Add|Existing Item, add file nhibernate-
mapping.xsd trong th mc NHibernate-3.1.0.GA-bin\lib download ,
vo schema folder
To th mc tn Domain trong d n
To cc lp Customer, Order v LineItem trong th mc Domain ca d
n. Cc lp ny c code nh sau
public class Customer
{
public virtual int Id { get; set; }
public virtual string CustomerName { get; set; }
}

public class Order
{
public virtual int Id { get; set; }
public virtual DateTime OrderDate { get; set; }
public virtual Customer Customer { get; set; }
public virtual IList<LineItem> LineItems { get; set; }
}

public class LineItem
{
public virtual int Id { get; set; }
public virtual int Quantity { get; set; }
public virtual decimal UnitPrice { get; set; }
public virtual string ProductCode { get; set; }
}
Add xml file vo folder Domain tn file l Customer.hbm.xml, v thuc tnh
Build Action ca file phi c set l Embeded Resource . cha code
nh sau :
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="XmlMappingSample"
namespace="XmlMappingSample.Domain">
<class name="Customer">
<id name="Id">
<generator class="hilo"/>
</id>
<property name="CustomerName"/>
</class>
</hibernate-mapping>
Tng t cch to cho Order.hbm.xml v LineItem.hbm.xml. nh set
thuc tnh Build Action ca file phi c set l Embeded Resource
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="XmlMappingSample"
namespace="XmlMappingSample.Domain">
<class name="Order">
<id name="Id">
<generator class="hilo"/>
</id>
<property name="OrderDate"/>
<many-to-one name="Customer" column="CustomerId"/>
<bag name="LineItems" inverse="true" >
<key column="OrderId"/>
<one-to-many class="LineItem"/>
</bag>
</class>
</hibernate-mapping>


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="XmlMappingSample"
namespace="XmlMappingSample.Domain">
<class name="LineItem">
<id name="Id">
<generator class="hilo"/>
</id>
<property name="Quantity"/>
<property name="UnitPrice"/>
<property name="ProductCode"/>
</class>
</hibernate-mapping>
Khai bo using trong file Program.cs
using System;
using NHibernate.Cfg;
using NHibernate.Cfg.Loquacious;
using NHibernate.Dialect;
using NHibernate.Driver;
using NHibernate.Tool.hbm2ddl;
using XmlMappingSample.Domain;

Trong phng thc Main ca Program.cs , thm code to i tng
NHibernate configuration dng fluent API c cung cp bi NHibernate
nh sau :
var configuration = new Configuration();
configuration.DataBaseIntegration(db =>
{
db.Dialect<MsSql2008Dialect>();
db.Driver<SqlClientDriver>();
});
Thm code add tt c XML mapping file cu hnh nh sau :
configuration.AddAssembly(typeof (Customer).Assembly);
Thm code to script to SQL schema nh sau :
var exporter = new SchemaExport(configuration);
exporter.Execute(true, false, false);
iv. XML Mapping
To 1 solution folder tn l Schema v add 2 file nhibernate-configuration.xsd v
nhibernate-mapping.xsd trong Nhibernate download vo folder Schema, nh hnh
sau :


nh ngha 1 mapping cho 1 entity, chng ta add mi 1 file XML vo d n.
File ny c ui m rng l .hbm.xml, v nn t tn ging tn ca entity. V d
Product.hbm.xml mapping Product entity.


Chng ta cng set thuc tnh Build Action ca XML file thnh Embeded
Resource nh hnh sau :


Ging nh bt k file XML document no khc, file XML mapping cng bt u
bng 1 khai bo XML, n gin l 1 th xml vi 2 thuc tnh version v encoding :
<?xml version=1.0 encoding=utf-8 ?>
Tip theo l th hibernate-mapping. Th ny c thuc tnh xmlns, namespace v
assembly ch ra ni m cha lp ta ang map
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="OrderingSystem.Domain"
assembly="OrderingSystem">
</hibernate-mapping>

8. Classes
Th tip theo cn nh ngha l th class, th ny bo cho NHibernate 2 th - 1
l ch ra lp m mapping document ny i din v 2 l ci bng trong csdl m lp
map ti.
Th lp c 2 thuc tnh (attributes) ta cn quan tm l name v table
<class name= table=>
</class>

9. Properties
Chng ta c th map cc thuc tnh (properties) ca lp vo cc trng trong
csdl dng th id v th property.
<property name="Name" type="String">
<column name="Name" length="255" sql-type="varchar" not-null="true"/>
</property>

Mt trong nhng thuc tnh m rng thng dng trong th id v property l
thuc tnh type. Bn c th ch cho NHibernate l bn dng kiu d liu no lu
thng tin ca lp, v d
<property name=Name type=String />

Nu bn khng dng thuc tnh type th NHibernate s t xc nh kiu d liu
theo phn x
10. ID columns
Thuc tnh u tin sau th class l bn mun map 1 thuc tnh Id. Th ny c
nhiu option bn la chn, v d n gin nht l :
<id name="Id">
<generator class="hilo"/>
</id>

Nhng dng ny ch cho NHibernate bit bn c 1 thuc tnh Id trong lp ca
bn, n c map xung 1 trng Id trong csdl. V y bn dng b to hilo cung
cp bi Nhibernate sinh s t tng cho trng Id.
Bn cng c th ch ra kiu d liu qua thuc tnh type.
<id name="Id" type=Int32>
<generator class="hilo"/>
</id>

Ngoi ra nu bn mun i tn ct trong csdl th lm nh sau
<id name="Id">
<column name="product_id"/>
<generator class="hilo"/>
</id>

Cc thuc tnh ph :
name : nh ngha tn ca ct trong csdl
length : chiu di ca trng trong csdl
sql-type : kiu d liu ca ct trong csdl
not-null : rng buc ct d liu c php null hay khng. Nu not-null=true th
khng c null

11. One-to-many relations
Gi s ta c entity l Order c thuc tnh(property) l LineItems, l tp hp ca
cc LineItem entity. Mi LineItem entity c 1 property l Order tham chiu n order
cha n. By gi ta map bn pha one ca mi quan h, tc l bn LineItem entity:
<many-to-one name="Order" class="Order" column="OrderId" not-null="true"/>
Trong th trn, thuc tnh name nh ngha tn ca property trong lp LineItem,
thuc tnh class nh ngha lp m property tham chiu, thuc tnh column ch ra tn
trng trong csdl, cha kha ngoi n bng Order. Cui cng not-null nh ngha l
kha ngoi OrderId khng c rng.
By gi ta map bn pha many. Trong Order mapping file, ta cn to 1 thnh
phn bag nm gi ton b lineItems. Mt bag l cch Nhibernate lu tr 1 tp hp k
th t cho php trng lp cc item. Ta c 1 thnh phn name nh ngha tn ca
property, thnh phn con key ch ra ct trong csdl m trng ny i din
<bag name="LineItems" inverse="true" cascade="all-delete-orphan">
<key column="OrderId"/>
<one-to-many class="LineItem"/>
</bag>
Bn s thy th one-to-many tng t nh th many-to-one ta to bn pha
one, bn bo Nhibernate v mi quan h inverse qua vic dng th inverse trong
thnh phn bag. Tc l bn chuyn quyn s hu mi quan h (relationship owner)
cho Order.
Th cascade ch cho NHibernate bit x l cc i tng nh th no khi chng
ta delete n.
Th lazy ch cho NHibernate dng lazy loading ngha l dng (record) s khng
c ko t database hay load vo b nh cho ti khi bn dng n, mc nh lazy
loading c m, bn mun tt th dng lazy =false.

12. Many-to-many relations
Quan h nhiu nhiu (many-to-many) c dng khc ph bin.

Trong hnh trn, bng BookAuthor dng kt 2 bng Book v Author.
NHibernate thng minh qun l nhng mi quan h nhiu nhiu
Ta biu din Authors trong lp Book vi 1 tp hp cc i tng Author nh sau
public IEnumerable<Author> Authors { get; set; }
Ta cng biu din Books trong lp Author vi 1 tp hp cc i tng Book nh
sau
public IEnumerable<Book> Books { get; set; }

Mapping mi quan h nhiu nhiu phc tp hn quan h 1 nhiu. Chng ta dng
1 th bag v c 1 th key bn trong. Chng ta cn thm thuc tnh table trong th bag
cho NHibernate bit bng no lu tr d liu quan h bn trong. Thay v dng thuc
tnh one-to-many v many-to-one, c 2 pha ta dng thnh phn many-to-many
Bn Book mapping file :
<bag name="Authors" table="BookAuthor">
<key column="BookId"/>
<many-to-many class="Author" column="AuthorId" />
</bag>
Bn Author mapping file :
<bag name="Books" table="BookAuthor">
<key column="AuthorId"/>
<many-to-many class="Book" column="BookId" />
</bag>

13. Mapping value objects
map cc thuc tnh ca value object, chng ta dng th component, v d
chng ta s map thuc tnh name ca entity Customer nh sau :
<component name="Name" class="Name">
<property name="LastName" length="50" not-null="true"/>
<property name="FirstName" length="50" not-null="true"/>
<property name="MiddleName" length="50"/>
</component>

14. Hng dn tng bc mapping vi XML
To mi 1 project, chn Console Application, t tn l
XmlMappingSample.
Add reference n assembly NHibernate.dll vo d n
Trong ca s Solution Explorer, click phi v solution, chn Add|New
Folder, t tn l Schema
Click phi Schema folder, chn Add|Existing Item, add file nhibernate-
mapping.xsd trong th mc NHibernate-3.1.0.GA-bin\lib download ,
vo schema folder
To th mc tn Domain trong d n
To cc lp Customer, Order v LineItem trong th mc Domain ca d
n. Cc lp ny c code nh sau
public class Customer
{
public virtual int Id { get; set; }
public virtual string CustomerName { get; set; }
}

public class Order
{
public virtual int Id { get; set; }
public virtual DateTime OrderDate { get; set; }
public virtual Customer Customer { get; set; }
public virtual IList<LineItem> LineItems { get; set; }
}

public class LineItem
{
public virtual int Id { get; set; }
public virtual int Quantity { get; set; }
public virtual decimal UnitPrice { get; set; }
public virtual string ProductCode { get; set; }
}
Add xml file vo folder Domain tn file l Customer.hbm.xml, v thuc tnh
Build Action ca file phi c set l Embeded Resource . cha code
nh sau :
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="XmlMappingSample"
namespace="XmlMappingSample.Domain">
<class name="Customer">
<id name="Id">
<generator class="hilo"/>
</id>
<property name="CustomerName"/>
</class>
</hibernate-mapping>
Tng t cch to cho Order.hbm.xml v LineItem.hbm.xml. nh set
thuc tnh Build Action ca file phi c set l Embeded Resource
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="XmlMappingSample"
namespace="XmlMappingSample.Domain">
<class name="Order">
<id name="Id">
<generator class="hilo"/>
</id>
<property name="OrderDate"/>
<many-to-one name="Customer" column="CustomerId"/>
<bag name="LineItems" inverse="true" >
<key column="OrderId"/>
<one-to-many class="LineItem"/>
</bag>
</class>
</hibernate-mapping>


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="XmlMappingSample"
namespace="XmlMappingSample.Domain">
<class name="LineItem">
<id name="Id">
<generator class="hilo"/>
</id>
<property name="Quantity"/>
<property name="UnitPrice"/>
<property name="ProductCode"/>
</class>
</hibernate-mapping>
Khai bo using trong file Program.cs
using System;
using NHibernate.Cfg;
using NHibernate.Cfg.Loquacious;
using NHibernate.Dialect;
using NHibernate.Driver;
using NHibernate.Tool.hbm2ddl;
using XmlMappingSample.Domain;

Trong phng thc Main ca Program.cs , thm code to i tng
NHibernate configuration dng fluent API c cung cp bi NHibernate
nh sau :
var configuration = new Configuration();
configuration.DataBaseIntegration(db =>
{
db.Dialect<MsSql2008Dialect>();
db.Driver<SqlClientDriver>();
});
Thm code add tt c XML mapping file cu hnh nh sau :
configuration.AddAssembly(typeof (Customer).Assembly);
Thm code to script to SQL schema nh sau :
var exporter = new SchemaExport(configuration);
exporter.Execute(true, false, false);






2.Kin trc NHibernate





Kin trc ca Hibernate bao gm 3 phn:
Qun l kt ni: Dch v qun l kt ni ca Hibernate cung cp vic qun l ht sc
hiu qu cc connection n database. Database connection lun l phn t v nhiu
ri ro trong vic kim sot, chng ta tn nhiu ti nguyn m v ng mt connection,
hn na, vic x l d liu phc tp v c th dn n qun st vic ng kt ni hoc
ng mt cch khng hp l. iu ny hon ton khng n gin v dn n mt ng
dng v. (Mt v d l ng nhiu kt ni trong 1 khi try...catch)
Qun l transation (giao tc):dch v qun l giao tc cung cp kh nng cho php user
thi hnh nhiu pht biu (statement) vo mt thi im.

Object relational mapping:
Cng ngh mapping d liu di database thnh d liu trn object. Hibernate s s dng
SELECT, INSERT, UPDATE v DELETE trn cc records v a thao tc di
database tng ng vi vic thao tc tn Object java.
Mc ch sau cng ca hibernate l ORM, nhng n phi thm vo cc dch v qun l
Connnection v Transaction v pht sinh t vn performance v s kh khn khi t mnh x l
nhng dch v ny trn Object c persistent. Hibernate buc phi s dng nhng cng c qun
l kt ni v transation ca bn th 3 hon thin m hnh ca mnh. Mt v d l Apache
DBCP thng c s dng trong connection pooling vi Hibernate.
Hibernate rt uyn chuyn, n c th tch hp vi nhng cng ngh khc nhn c s h tr
tt nht, tiu biu c Spring.

You might also like