SlideShare una empresa de Scribd logo
1 de 32
Descargar para leer sin conexión
Samnang Chhun (samnang@wowkhmer.com)
Software Engineer,
http://tech.wowkhmer.com
Introduction to Object-Relational Mapping
Introduction to NHibernate
NHibernate Basics
Mapping Inheritance hierarchies
Advanced Querying
Additional Reading




                                            2
3
Object-relational mapping (aka ORM, O/RM, and O/R
mapping) is a programming technique for converting data
between incompatible type systems in relational databases
and object-oriented programming languages (Wikipedia)
   Objects are hierarchical
   Databases are relational


                        ORM

                                            Relational
     Objects




                                                            4
Performance or Scalability
Productivity: less code to write/maintain
Abstraction: transient to different DB technologies
Simplification and Consistency
Quality: depending on the product




                                                      5
ADO.NET Entity Framework (released with .NET 3.5 SP1)
Business Logic Toolkit for .NET
Castle ActiveRecord
IBatis.Net
LightSpeed
Linq (Language Integrated Query)
LLBLGen
LLBLGen Pro
NHibernate
Neo …etc



                                                        6
7
Initially developed for Java
    created in late 2001 by Gavin King
    absorbed by the JBoss Group / Red Hat
Ported to .NET 1.1, 2.0, 3.5
    Resulting product called “NHibernate”
All popular databases supported
    Oracle, SQL Server, DB2, SQLite, PostgreSQL,
    MySQL, Sybase, Firebird, …
XML-based configuration files
Good community support
Free/open source - NHibernate is licensed under the
LGPL (Lesser GNU Public License)

                                                      8
RDBMS

        9
ISessionFactory
   One per database (or application)
   Expensive to create
      Reads configuration
ISession
   Portal to the database
   Saves, retrieves
ITransaction
   Encapsulates database transactions


  SessionFactory
                   Session
                               Transaction
                                             10
11
12
<class> declare a persistent class
<id> defines the mapping from that property to the
primary key column
    Specifies strategy
<property> declares a persistent property of the class
<component> maps properties of a child object to
columns of the table of a parent class.
Associations
    One-to-Many
    Many-to-One
    Many-to-Many
    One-to-One (uncommon)

                                                         13
Element   Description                  .NET Type
                                       Iesi.Collections.ISet
<set>     An unordered collection
                                       Iesi.Collections.Generic.ISet<T>
          that does not allow
          duplicates.
                                       System.Collections.IList
<list>    An ordered collection that
                                       System.Collections.Generic.IList<T>
          allows duplicates


                                       System.Collections.IList
<bag>     An unordered collection
                                       System.Collections.Generic.IList<T>
          that allow duplicatd




                                                                          14
15
<class name=quot;Animalquot;>
   <id name=quot;Idquot;>
       <generator class=quot;nativequot; />
   </id>
   <discriminator column=quot;AnimalTypequot; length=quot;5quot; />
   <property name=quot;Namequot; />

   <subclass name=quot;Dogquot; discriminator-value=quot;Dogquot;>
       <property name=quot;Breedquot; />
   </subclass>
   <subclass name=“Frogquot; discriminator-value=“Frogquot;>
       <property name=“TongueLengthquot; />
   </subclass>
</class>




                                                       16
Pros
   Simple approach
   Easy to add new classes, you just need to add new
   columns for the additional data
   Data access is fast because the data is in one table
   Ad-hoc reporting is very easy because all of the data
   is found in one table.
Cons
   Coupling within the class hierarchy is increased
   because all classes are directly coupled to the same
   table. A change in one class can affect the table
   which can then affect the other classes in the
   hierarchy
   Space potentially wasted in the database
   Table can grow quickly for large hierarchies.         17
<class name=quot;Animalquot;>
   <id name=quot;Idquot;>
       <generator class=quot;nativequot; />
   </id>
   <property name=quot;Namequot; />
   <joined-subclass name=quot;Dogquot;>
       <key column=quot;Idquot; />
       <property name=quot;Breedquot; />
   </joined-subclass>
   <joined-subclass name=quot;Frogquot;>
       <key column=quot;Idquot; />
       <property name=quot;TongueLengthquot; />
   </joined-subclass>
</class>




                                          18
Pros
   Easy to understand because of the one-to-one
   mapping
   Very easy to modify superclasses and add new
   subclasses as you merely need to modify/add one
   table
   Data size grows in direct proportion to growth in the
   number of objects.
Cons
   There are many tables in the database, one for every
   class (plus tables to maintain relationships)
   Potentially takes longer to read and write data using
   this technique because you need to access multiple
   tables
   Ad-hoc reporting on your database is difficult, unless
   you add views to simulate the desired tables.            19
<class name=quot;Frogquot;>
   <id name=quot;Idquot;>
       <generator class=quot;nativequot; />
   </id>
   <property name=quot;Namequot; />
   <property name=quot;TongueLengthquot; />
</class>

<class name=quot;Dogquot;>
   <id name=quot;Idquot;>
       <generator class=quot;nativequot; />
   </id>
   <property name=quot;Namequot; />
   <property name=quot;Breedquot; />
</class>



                                      20
Pros
   Easy to do ad-hoc reporting as all the data you need
   about a single class is stored in only one table
   Good performance to access a single object’s data.
Cons
   When you modify a class you need to modify its table
   and the table of any of its subclasses.




                                                          21
22
23
Object oriented querying
    Increase compile-time syntax-checking
    Easy to write
    Hard to read
ICriteria crit = sess.CreateCriteria(typeof(Cat));
crit.SetMaxResults(50);
List topCats = crit.List();


IList cats = sess.CreateCriteria(typeof(Cat))
 .Add( Restrictions.Like(quot;Namequot;, quot;Fritz%quot;))
 .Add( Restrictions.Between(quot;Weightquot;, minWeight,
      maxWeight))
 .List();
                                                     24
String based querying
Object-Oriented SQL
Similar to SQL
Speak in terms of objects
Case sensitive
Very flexible
Zero compile-time syntax-checking

• from Customer c where c.Name like :name

• select count(*) from Customer c



                                            25
Powerful way to (simply) return a group of like objects
from the DB.
   Wonderfully simple to work with
   Great way to quickly process a “…where
   A=<something> and B=<something> and
   C=<something>…”
Cat cat = new Cat();
cat.Sex = 'F';
cat.Color = Color.Black;
List results = session.CreateCriteria(typeof(Cat))
      .Add( Example.Create(cat) )
      .List();



                                                          26
You can submit SQL statements to NHibernate if the
 other methods of querying a database do not fit your
 needs
 utilize database specific features

• sess.CreateSQLQuery(quot;SELECT * FROM CATSquot;)
      .AddScalar(quot;IDquot;, NHibernateUtil.Int32)
      .AddScalar(quot;NAMEquot;, NHibernateUtil.String)
      .AddScalar(quot;BIRTHDATEquot;, NHibernateUtil.Date);


• sess.CreateSQLQuery(quot;SELECT * FROM CATSquot;)
            .AddEntity(typeof(Cat));


                                                        27
28
29
Nhibernate.Contrib
   Mapping.Attributes
   Cache
   Search
   Validator
   Burrow
   LINQ to NHibernate
   Shards
Fluent Interface to NHibernate




                                 30
http://en.wikipedia.org/wiki/Object-relational_mapping
NHibernate in Action
NHibernate Reference Documentation 1.2.0
http://code.google.com/p/sharp-architecture/
http://www.codeproject.com/KB/database/Nhibernate_M
ade_Simple.aspx
http://www.codeproject.com/KB/architecture/NHibernate
BestPractices.aspx
www.hibernate.org
NHibernate FAQ
Summer of NHibernate


                                                     31
Nhibernatethe Orm For Net Platform 1226744632929962 8

Más contenido relacionado

La actualidad más candente

UKOUG 2010 (Birmingham) - XML Indexing strategies - Choosing the Right Index ...
UKOUG 2010 (Birmingham) - XML Indexing strategies - Choosing the Right Index ...UKOUG 2010 (Birmingham) - XML Indexing strategies - Choosing the Right Index ...
UKOUG 2010 (Birmingham) - XML Indexing strategies - Choosing the Right Index ...Marco Gralike
 
Miracle Open World 2011 - XML Index Strategies
Miracle Open World 2011  -  XML Index StrategiesMiracle Open World 2011  -  XML Index Strategies
Miracle Open World 2011 - XML Index StrategiesMarco Gralike
 
Patni Hibernate
Patni   HibernatePatni   Hibernate
Patni Hibernatepatinijava
 
Introduction to Hibernate Framework
Introduction to Hibernate FrameworkIntroduction to Hibernate Framework
Introduction to Hibernate FrameworkRaveendra R
 
UKOUG Tech14 - Using Database In-Memory Column Store with Complex Datatypes
UKOUG Tech14 - Using Database In-Memory Column Store with Complex DatatypesUKOUG Tech14 - Using Database In-Memory Column Store with Complex Datatypes
UKOUG Tech14 - Using Database In-Memory Column Store with Complex DatatypesMarco Gralike
 
Hibernate inheritance and relational mappings with examples
Hibernate inheritance and relational mappings with examplesHibernate inheritance and relational mappings with examples
Hibernate inheritance and relational mappings with examplesEr. Gaurav Kumar
 
Hotsos 2013 - Creating Structure in Unstructured Data
Hotsos 2013 - Creating Structure in Unstructured DataHotsos 2013 - Creating Structure in Unstructured Data
Hotsos 2013 - Creating Structure in Unstructured DataMarco Gralike
 
OPP2010 (Brussels) - Programming with XML in PL/SQL - Part 1
OPP2010 (Brussels) - Programming with XML in PL/SQL - Part 1OPP2010 (Brussels) - Programming with XML in PL/SQL - Part 1
OPP2010 (Brussels) - Programming with XML in PL/SQL - Part 1Marco Gralike
 
Compass Framework
Compass FrameworkCompass Framework
Compass FrameworkLukas Vlcek
 
OPP2010 (Brussels) - Programming with XML in PL/SQL - Part 2
OPP2010 (Brussels) - Programming with XML in PL/SQL - Part 2OPP2010 (Brussels) - Programming with XML in PL/SQL - Part 2
OPP2010 (Brussels) - Programming with XML in PL/SQL - Part 2Marco Gralike
 
BGOUG 2012 - XML Index Strategies
BGOUG 2012 - XML Index StrategiesBGOUG 2012 - XML Index Strategies
BGOUG 2012 - XML Index StrategiesMarco Gralike
 
XML Amsterdam - Creating structure in unstructured data
XML Amsterdam - Creating structure in unstructured dataXML Amsterdam - Creating structure in unstructured data
XML Amsterdam - Creating structure in unstructured dataMarco Gralike
 
Hibernate Developer Reference
Hibernate Developer ReferenceHibernate Developer Reference
Hibernate Developer ReferenceMuthuselvam RS
 
ODTUG Webcast - Thinking Clearly about XML
ODTUG Webcast - Thinking Clearly about XMLODTUG Webcast - Thinking Clearly about XML
ODTUG Webcast - Thinking Clearly about XMLMarco Gralike
 
Database Programming Techniques
Database Programming TechniquesDatabase Programming Techniques
Database Programming TechniquesRaji Ghawi
 

La actualidad más candente (20)

UKOUG 2010 (Birmingham) - XML Indexing strategies - Choosing the Right Index ...
UKOUG 2010 (Birmingham) - XML Indexing strategies - Choosing the Right Index ...UKOUG 2010 (Birmingham) - XML Indexing strategies - Choosing the Right Index ...
UKOUG 2010 (Birmingham) - XML Indexing strategies - Choosing the Right Index ...
 
Hibernate
HibernateHibernate
Hibernate
 
Miracle Open World 2011 - XML Index Strategies
Miracle Open World 2011  -  XML Index StrategiesMiracle Open World 2011  -  XML Index Strategies
Miracle Open World 2011 - XML Index Strategies
 
Hibernate in Nutshell
Hibernate in NutshellHibernate in Nutshell
Hibernate in Nutshell
 
Patni Hibernate
Patni   HibernatePatni   Hibernate
Patni Hibernate
 
Introduction to Hibernate Framework
Introduction to Hibernate FrameworkIntroduction to Hibernate Framework
Introduction to Hibernate Framework
 
UKOUG Tech14 - Using Database In-Memory Column Store with Complex Datatypes
UKOUG Tech14 - Using Database In-Memory Column Store with Complex DatatypesUKOUG Tech14 - Using Database In-Memory Column Store with Complex Datatypes
UKOUG Tech14 - Using Database In-Memory Column Store with Complex Datatypes
 
Hibernate inheritance and relational mappings with examples
Hibernate inheritance and relational mappings with examplesHibernate inheritance and relational mappings with examples
Hibernate inheritance and relational mappings with examples
 
Hibernate tutorial
Hibernate tutorialHibernate tutorial
Hibernate tutorial
 
Hotsos 2013 - Creating Structure in Unstructured Data
Hotsos 2013 - Creating Structure in Unstructured DataHotsos 2013 - Creating Structure in Unstructured Data
Hotsos 2013 - Creating Structure in Unstructured Data
 
OPP2010 (Brussels) - Programming with XML in PL/SQL - Part 1
OPP2010 (Brussels) - Programming with XML in PL/SQL - Part 1OPP2010 (Brussels) - Programming with XML in PL/SQL - Part 1
OPP2010 (Brussels) - Programming with XML in PL/SQL - Part 1
 
Compass Framework
Compass FrameworkCompass Framework
Compass Framework
 
ORM JPA
ORM JPAORM JPA
ORM JPA
 
OPP2010 (Brussels) - Programming with XML in PL/SQL - Part 2
OPP2010 (Brussels) - Programming with XML in PL/SQL - Part 2OPP2010 (Brussels) - Programming with XML in PL/SQL - Part 2
OPP2010 (Brussels) - Programming with XML in PL/SQL - Part 2
 
Hibernate in Action
Hibernate in ActionHibernate in Action
Hibernate in Action
 
BGOUG 2012 - XML Index Strategies
BGOUG 2012 - XML Index StrategiesBGOUG 2012 - XML Index Strategies
BGOUG 2012 - XML Index Strategies
 
XML Amsterdam - Creating structure in unstructured data
XML Amsterdam - Creating structure in unstructured dataXML Amsterdam - Creating structure in unstructured data
XML Amsterdam - Creating structure in unstructured data
 
Hibernate Developer Reference
Hibernate Developer ReferenceHibernate Developer Reference
Hibernate Developer Reference
 
ODTUG Webcast - Thinking Clearly about XML
ODTUG Webcast - Thinking Clearly about XMLODTUG Webcast - Thinking Clearly about XML
ODTUG Webcast - Thinking Clearly about XML
 
Database Programming Techniques
Database Programming TechniquesDatabase Programming Techniques
Database Programming Techniques
 

Destacado

Tarea 2 Segundo Parcial Mario Meza
Tarea 2 Segundo Parcial Mario MezaTarea 2 Segundo Parcial Mario Meza
Tarea 2 Segundo Parcial Mario MezaMario Meza Enriquez
 
Simcoe County OGS - Death and Digital Legacy
Simcoe County OGS - Death and Digital LegacySimcoe County OGS - Death and Digital Legacy
Simcoe County OGS - Death and Digital LegacyAdele McAlear
 
Twitter Tools For Strategic Marketing
Twitter Tools For Strategic MarketingTwitter Tools For Strategic Marketing
Twitter Tools For Strategic MarketingAdele McAlear
 
Hack Politics - Lighting Talk
Hack Politics - Lighting TalkHack Politics - Lighting Talk
Hack Politics - Lighting TalkAitor Rey
 
Charting Your Course With Enterprise Architecture - Andy Blumenthal
Charting Your Course With Enterprise Architecture - Andy BlumenthalCharting Your Course With Enterprise Architecture - Andy Blumenthal
Charting Your Course With Enterprise Architecture - Andy BlumenthalAndy (Avraham) Blumenthal
 
Death Digital Legacy - Ignite Ottawa
Death Digital Legacy - Ignite OttawaDeath Digital Legacy - Ignite Ottawa
Death Digital Legacy - Ignite OttawaAdele McAlear
 
Creating Friend Lists on Facebook
Creating Friend Lists on FacebookCreating Friend Lists on Facebook
Creating Friend Lists on Facebookmdpr
 

Destacado (9)

Tarea 2 Segundo Parcial Mario Meza
Tarea 2 Segundo Parcial Mario MezaTarea 2 Segundo Parcial Mario Meza
Tarea 2 Segundo Parcial Mario Meza
 
Simcoe County OGS - Death and Digital Legacy
Simcoe County OGS - Death and Digital LegacySimcoe County OGS - Death and Digital Legacy
Simcoe County OGS - Death and Digital Legacy
 
Twitter Tools For Strategic Marketing
Twitter Tools For Strategic MarketingTwitter Tools For Strategic Marketing
Twitter Tools For Strategic Marketing
 
Hack Politics - Lighting Talk
Hack Politics - Lighting TalkHack Politics - Lighting Talk
Hack Politics - Lighting Talk
 
Charting Your Course With Enterprise Architecture - Andy Blumenthal
Charting Your Course With Enterprise Architecture - Andy BlumenthalCharting Your Course With Enterprise Architecture - Andy Blumenthal
Charting Your Course With Enterprise Architecture - Andy Blumenthal
 
Death Digital Legacy - Ignite Ottawa
Death Digital Legacy - Ignite OttawaDeath Digital Legacy - Ignite Ottawa
Death Digital Legacy - Ignite Ottawa
 
The Finished Product
The Finished Product The Finished Product
The Finished Product
 
Creating Friend Lists on Facebook
Creating Friend Lists on FacebookCreating Friend Lists on Facebook
Creating Friend Lists on Facebook
 
Ge juego sgc v1
Ge juego sgc v1Ge juego sgc v1
Ge juego sgc v1
 

Similar a Nhibernatethe Orm For Net Platform 1226744632929962 8

Persisting Your Objects In The Database World @ AlphaCSP Professional OSS Con...
Persisting Your Objects In The Database World @ AlphaCSP Professional OSS Con...Persisting Your Objects In The Database World @ AlphaCSP Professional OSS Con...
Persisting Your Objects In The Database World @ AlphaCSP Professional OSS Con...Baruch Sadogursky
 
Practical catalyst
Practical catalystPractical catalyst
Practical catalystdwm042
 
Building AOL's High Performance, Enterprise Wide Mail Application With Silver...
Building AOL's High Performance, Enterprise Wide Mail Application With Silver...Building AOL's High Performance, Enterprise Wide Mail Application With Silver...
Building AOL's High Performance, Enterprise Wide Mail Application With Silver...goodfriday
 
ASP.NET 08 - Data Binding And Representation
ASP.NET 08 - Data Binding And RepresentationASP.NET 08 - Data Binding And Representation
ASP.NET 08 - Data Binding And RepresentationRandy Connolly
 
Tutorial On Database Management System
Tutorial On Database Management SystemTutorial On Database Management System
Tutorial On Database Management Systempsathishcs
 
Linq 1224887336792847 9
Linq 1224887336792847 9Linq 1224887336792847 9
Linq 1224887336792847 9google
 
Intro to Rails ActiveRecord
Intro to Rails ActiveRecordIntro to Rails ActiveRecord
Intro to Rails ActiveRecordMark Menard
 
Defense Against the Dark Arts: Protecting Your Data from ORMs
Defense Against the Dark Arts: Protecting Your Data from ORMsDefense Against the Dark Arts: Protecting Your Data from ORMs
Defense Against the Dark Arts: Protecting Your Data from ORMsVanessa Hurst
 
Introducing Struts 2
Introducing Struts 2Introducing Struts 2
Introducing Struts 2wiradikusuma
 
Linq To The Enterprise
Linq To The EnterpriseLinq To The Enterprise
Linq To The EnterpriseDaniel Egan
 
Extending MySQL Enterprise Monitor
Extending MySQL Enterprise MonitorExtending MySQL Enterprise Monitor
Extending MySQL Enterprise MonitorMark Leith
 
Zend framework 03 - singleton factory data mapper caching logging
Zend framework 03 - singleton factory data mapper caching loggingZend framework 03 - singleton factory data mapper caching logging
Zend framework 03 - singleton factory data mapper caching loggingTricode (part of Dept)
 
Java Web Programming on Google Cloud Platform [2/3] : Datastore
Java Web Programming on Google Cloud Platform [2/3] : DatastoreJava Web Programming on Google Cloud Platform [2/3] : Datastore
Java Web Programming on Google Cloud Platform [2/3] : DatastoreIMC Institute
 
Struts Tags Speakernoted
Struts Tags SpeakernotedStruts Tags Speakernoted
Struts Tags SpeakernotedHarjinder Singh
 
NoSQL Endgame DevoxxUA Conference 2020
NoSQL Endgame DevoxxUA Conference 2020NoSQL Endgame DevoxxUA Conference 2020
NoSQL Endgame DevoxxUA Conference 2020Thodoris Bais
 
Intake 38 data access 5
Intake 38 data access 5Intake 38 data access 5
Intake 38 data access 5Mahmoud Ouf
 
Wcf data services
Wcf data servicesWcf data services
Wcf data servicesEyal Vardi
 

Similar a Nhibernatethe Orm For Net Platform 1226744632929962 8 (20)

Persisting Your Objects In The Database World @ AlphaCSP Professional OSS Con...
Persisting Your Objects In The Database World @ AlphaCSP Professional OSS Con...Persisting Your Objects In The Database World @ AlphaCSP Professional OSS Con...
Persisting Your Objects In The Database World @ AlphaCSP Professional OSS Con...
 
Practical catalyst
Practical catalystPractical catalyst
Practical catalyst
 
Building AOL's High Performance, Enterprise Wide Mail Application With Silver...
Building AOL's High Performance, Enterprise Wide Mail Application With Silver...Building AOL's High Performance, Enterprise Wide Mail Application With Silver...
Building AOL's High Performance, Enterprise Wide Mail Application With Silver...
 
ASP.NET 08 - Data Binding And Representation
ASP.NET 08 - Data Binding And RepresentationASP.NET 08 - Data Binding And Representation
ASP.NET 08 - Data Binding And Representation
 
Tutorial On Database Management System
Tutorial On Database Management SystemTutorial On Database Management System
Tutorial On Database Management System
 
Linq 1224887336792847 9
Linq 1224887336792847 9Linq 1224887336792847 9
Linq 1224887336792847 9
 
Intro to Rails ActiveRecord
Intro to Rails ActiveRecordIntro to Rails ActiveRecord
Intro to Rails ActiveRecord
 
Defense Against the Dark Arts: Protecting Your Data from ORMs
Defense Against the Dark Arts: Protecting Your Data from ORMsDefense Against the Dark Arts: Protecting Your Data from ORMs
Defense Against the Dark Arts: Protecting Your Data from ORMs
 
Introducing Struts 2
Introducing Struts 2Introducing Struts 2
Introducing Struts 2
 
Linq To The Enterprise
Linq To The EnterpriseLinq To The Enterprise
Linq To The Enterprise
 
Extending MySQL Enterprise Monitor
Extending MySQL Enterprise MonitorExtending MySQL Enterprise Monitor
Extending MySQL Enterprise Monitor
 
Zend framework 03 - singleton factory data mapper caching logging
Zend framework 03 - singleton factory data mapper caching loggingZend framework 03 - singleton factory data mapper caching logging
Zend framework 03 - singleton factory data mapper caching logging
 
Java Web Programming on Google Cloud Platform [2/3] : Datastore
Java Web Programming on Google Cloud Platform [2/3] : DatastoreJava Web Programming on Google Cloud Platform [2/3] : Datastore
Java Web Programming on Google Cloud Platform [2/3] : Datastore
 
Spring data requery
Spring data requerySpring data requery
Spring data requery
 
Struts Tags Speakernoted
Struts Tags SpeakernotedStruts Tags Speakernoted
Struts Tags Speakernoted
 
NoSQL Endgame DevoxxUA Conference 2020
NoSQL Endgame DevoxxUA Conference 2020NoSQL Endgame DevoxxUA Conference 2020
NoSQL Endgame DevoxxUA Conference 2020
 
Intake 38 data access 5
Intake 38 data access 5Intake 38 data access 5
Intake 38 data access 5
 
Struts2
Struts2Struts2
Struts2
 
Wcf data services
Wcf data servicesWcf data services
Wcf data services
 
Introduction to Datastore
Introduction to DatastoreIntroduction to Datastore
Introduction to Datastore
 

Último

Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfOverkill Security
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKJago de Vreede
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 

Último (20)

Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 

Nhibernatethe Orm For Net Platform 1226744632929962 8

  • 1. Samnang Chhun (samnang@wowkhmer.com) Software Engineer, http://tech.wowkhmer.com
  • 2. Introduction to Object-Relational Mapping Introduction to NHibernate NHibernate Basics Mapping Inheritance hierarchies Advanced Querying Additional Reading 2
  • 3. 3
  • 4. Object-relational mapping (aka ORM, O/RM, and O/R mapping) is a programming technique for converting data between incompatible type systems in relational databases and object-oriented programming languages (Wikipedia) Objects are hierarchical Databases are relational ORM Relational Objects 4
  • 5. Performance or Scalability Productivity: less code to write/maintain Abstraction: transient to different DB technologies Simplification and Consistency Quality: depending on the product 5
  • 6. ADO.NET Entity Framework (released with .NET 3.5 SP1) Business Logic Toolkit for .NET Castle ActiveRecord IBatis.Net LightSpeed Linq (Language Integrated Query) LLBLGen LLBLGen Pro NHibernate Neo …etc 6
  • 7. 7
  • 8. Initially developed for Java created in late 2001 by Gavin King absorbed by the JBoss Group / Red Hat Ported to .NET 1.1, 2.0, 3.5 Resulting product called “NHibernate” All popular databases supported Oracle, SQL Server, DB2, SQLite, PostgreSQL, MySQL, Sybase, Firebird, … XML-based configuration files Good community support Free/open source - NHibernate is licensed under the LGPL (Lesser GNU Public License) 8
  • 9. RDBMS 9
  • 10. ISessionFactory One per database (or application) Expensive to create Reads configuration ISession Portal to the database Saves, retrieves ITransaction Encapsulates database transactions SessionFactory Session Transaction 10
  • 11. 11
  • 12. 12
  • 13. <class> declare a persistent class <id> defines the mapping from that property to the primary key column Specifies strategy <property> declares a persistent property of the class <component> maps properties of a child object to columns of the table of a parent class. Associations One-to-Many Many-to-One Many-to-Many One-to-One (uncommon) 13
  • 14. Element Description .NET Type Iesi.Collections.ISet <set> An unordered collection Iesi.Collections.Generic.ISet<T> that does not allow duplicates. System.Collections.IList <list> An ordered collection that System.Collections.Generic.IList<T> allows duplicates System.Collections.IList <bag> An unordered collection System.Collections.Generic.IList<T> that allow duplicatd 14
  • 15. 15
  • 16. <class name=quot;Animalquot;> <id name=quot;Idquot;> <generator class=quot;nativequot; /> </id> <discriminator column=quot;AnimalTypequot; length=quot;5quot; /> <property name=quot;Namequot; /> <subclass name=quot;Dogquot; discriminator-value=quot;Dogquot;> <property name=quot;Breedquot; /> </subclass> <subclass name=“Frogquot; discriminator-value=“Frogquot;> <property name=“TongueLengthquot; /> </subclass> </class> 16
  • 17. Pros Simple approach Easy to add new classes, you just need to add new columns for the additional data Data access is fast because the data is in one table Ad-hoc reporting is very easy because all of the data is found in one table. Cons Coupling within the class hierarchy is increased because all classes are directly coupled to the same table. A change in one class can affect the table which can then affect the other classes in the hierarchy Space potentially wasted in the database Table can grow quickly for large hierarchies. 17
  • 18. <class name=quot;Animalquot;> <id name=quot;Idquot;> <generator class=quot;nativequot; /> </id> <property name=quot;Namequot; /> <joined-subclass name=quot;Dogquot;> <key column=quot;Idquot; /> <property name=quot;Breedquot; /> </joined-subclass> <joined-subclass name=quot;Frogquot;> <key column=quot;Idquot; /> <property name=quot;TongueLengthquot; /> </joined-subclass> </class> 18
  • 19. Pros Easy to understand because of the one-to-one mapping Very easy to modify superclasses and add new subclasses as you merely need to modify/add one table Data size grows in direct proportion to growth in the number of objects. Cons There are many tables in the database, one for every class (plus tables to maintain relationships) Potentially takes longer to read and write data using this technique because you need to access multiple tables Ad-hoc reporting on your database is difficult, unless you add views to simulate the desired tables. 19
  • 20. <class name=quot;Frogquot;> <id name=quot;Idquot;> <generator class=quot;nativequot; /> </id> <property name=quot;Namequot; /> <property name=quot;TongueLengthquot; /> </class> <class name=quot;Dogquot;> <id name=quot;Idquot;> <generator class=quot;nativequot; /> </id> <property name=quot;Namequot; /> <property name=quot;Breedquot; /> </class> 20
  • 21. Pros Easy to do ad-hoc reporting as all the data you need about a single class is stored in only one table Good performance to access a single object’s data. Cons When you modify a class you need to modify its table and the table of any of its subclasses. 21
  • 22. 22
  • 23. 23
  • 24. Object oriented querying Increase compile-time syntax-checking Easy to write Hard to read ICriteria crit = sess.CreateCriteria(typeof(Cat)); crit.SetMaxResults(50); List topCats = crit.List(); IList cats = sess.CreateCriteria(typeof(Cat)) .Add( Restrictions.Like(quot;Namequot;, quot;Fritz%quot;)) .Add( Restrictions.Between(quot;Weightquot;, minWeight, maxWeight)) .List(); 24
  • 25. String based querying Object-Oriented SQL Similar to SQL Speak in terms of objects Case sensitive Very flexible Zero compile-time syntax-checking • from Customer c where c.Name like :name • select count(*) from Customer c 25
  • 26. Powerful way to (simply) return a group of like objects from the DB. Wonderfully simple to work with Great way to quickly process a “…where A=<something> and B=<something> and C=<something>…” Cat cat = new Cat(); cat.Sex = 'F'; cat.Color = Color.Black; List results = session.CreateCriteria(typeof(Cat)) .Add( Example.Create(cat) ) .List(); 26
  • 27. You can submit SQL statements to NHibernate if the other methods of querying a database do not fit your needs utilize database specific features • sess.CreateSQLQuery(quot;SELECT * FROM CATSquot;) .AddScalar(quot;IDquot;, NHibernateUtil.Int32) .AddScalar(quot;NAMEquot;, NHibernateUtil.String) .AddScalar(quot;BIRTHDATEquot;, NHibernateUtil.Date); • sess.CreateSQLQuery(quot;SELECT * FROM CATSquot;) .AddEntity(typeof(Cat)); 27
  • 28. 28
  • 29. 29
  • 30. Nhibernate.Contrib Mapping.Attributes Cache Search Validator Burrow LINQ to NHibernate Shards Fluent Interface to NHibernate 30
  • 31. http://en.wikipedia.org/wiki/Object-relational_mapping NHibernate in Action NHibernate Reference Documentation 1.2.0 http://code.google.com/p/sharp-architecture/ http://www.codeproject.com/KB/database/Nhibernate_M ade_Simple.aspx http://www.codeproject.com/KB/architecture/NHibernate BestPractices.aspx www.hibernate.org NHibernate FAQ Summer of NHibernate 31