nHibernate QueryAlbert Kuo
How nHibernation work?nHibernateQuerybasic queryRestrictionString matcherDynamic queriesSubqueriesUsing Dynamic InstitationAgenda2
How nHibernation work?Utilize session to manipulate VOsCriteriaSessiontablesVOsVOsVOsConfigurationSessionFactoryVOs3
How nHibernation work? – cont.createupdate4
How nHibernation work? – cont.deletefind5
nHibernate QueryCriteria APIHibernate Query LanguageObject oriented queryingIncrease compile-time syntax-checkingEasy to writeHard to readString-based queryingObject-Oriented SQLSimilar to SQLSpeak in terms of objectsCase sensitiveVery flexibleZero compile-time syntax-checking6
Using the ICriteriainterface:ICriteriacriteria = session.CreateCriteria(typeof(Cases));Generate SQL statement:select cases0_.caseId as caseId0_, cases0_.caseDesc as caseDesc0_, cases0_.caseTime as caseTime0_, cases0_.caseType as caseType0_, cases0_.caseAddr as caseAddr0_, cases0_.reporterStaff as reporter6_0_, cases0_.reporterPhone as reporter7_0_, cases0_.reporterAddr as reporter8_0_, cases0_.receiverStaff as receiver9_0_, cases0_.receiveTime as receive10_0_, cases0_.caseState as caseState0_, cases0_.updtDate as updtDate0_, cases0_.county as county0_, cases0_.town as town0_, cases0_.caseClass as caseClass0_ from Cases cases0_nHibernate Query – basic query7
You usually don’t want to retrieve all instances of a class when you run a query. Instead, you want to express some constraints on the property values of your objects, so only a subset of objects is retrieved. This is called restriction.nHibernate Query – Restriction8
The like operator allows wildcard searches, where the wildcard symbols are % and _nHibernate Query – String matcher9
It’s common for queries to be built programmatically by combining several optional query criteria depending on user input.nHibernate Query – Dynamic queries10
11
nHibernate Query – Subqueries12
If you find working with arrays of values a little cumbersome, NHibernate let’s you use dynamic instantiation and define a class to represent each row of results. You can do this using the HQL select new construct:The custom ItemRow class doesn’t have to be a persistent class that has its own mapping file. But in order for NHibernate to “see” it, you need to import it usingnHibernate Query – Using Dynamic Institation13
Chapter 11. HQL: The Hibernate Query Languagehttps://www.hibernate.org/hib_docs/nhibernate/html/queryhql.htmlNHibernate in Actionhttp://www.manning.com/kuate/Reference14

nHibernate Query

  • 1.
  • 2.
    How nHibernation work?nHibernateQuerybasicqueryRestrictionString matcherDynamic queriesSubqueriesUsing Dynamic InstitationAgenda2
  • 3.
    How nHibernation work?Utilizesession to manipulate VOsCriteriaSessiontablesVOsVOsVOsConfigurationSessionFactoryVOs3
  • 4.
    How nHibernation work?– cont.createupdate4
  • 5.
    How nHibernation work?– cont.deletefind5
  • 6.
    nHibernate QueryCriteria APIHibernateQuery LanguageObject oriented queryingIncrease compile-time syntax-checkingEasy to writeHard to readString-based queryingObject-Oriented SQLSimilar to SQLSpeak in terms of objectsCase sensitiveVery flexibleZero compile-time syntax-checking6
  • 7.
    Using the ICriteriainterface:ICriteriacriteria= session.CreateCriteria(typeof(Cases));Generate SQL statement:select cases0_.caseId as caseId0_, cases0_.caseDesc as caseDesc0_, cases0_.caseTime as caseTime0_, cases0_.caseType as caseType0_, cases0_.caseAddr as caseAddr0_, cases0_.reporterStaff as reporter6_0_, cases0_.reporterPhone as reporter7_0_, cases0_.reporterAddr as reporter8_0_, cases0_.receiverStaff as receiver9_0_, cases0_.receiveTime as receive10_0_, cases0_.caseState as caseState0_, cases0_.updtDate as updtDate0_, cases0_.county as county0_, cases0_.town as town0_, cases0_.caseClass as caseClass0_ from Cases cases0_nHibernate Query – basic query7
  • 8.
    You usually don’twant to retrieve all instances of a class when you run a query. Instead, you want to express some constraints on the property values of your objects, so only a subset of objects is retrieved. This is called restriction.nHibernate Query – Restriction8
  • 9.
    The like operatorallows wildcard searches, where the wildcard symbols are % and _nHibernate Query – String matcher9
  • 10.
    It’s common forqueries to be built programmatically by combining several optional query criteria depending on user input.nHibernate Query – Dynamic queries10
  • 11.
  • 12.
  • 13.
    If you findworking with arrays of values a little cumbersome, NHibernate let’s you use dynamic instantiation and define a class to represent each row of results. You can do this using the HQL select new construct:The custom ItemRow class doesn’t have to be a persistent class that has its own mapping file. But in order for NHibernate to “see” it, you need to import it usingnHibernate Query – Using Dynamic Institation13
  • 14.
    Chapter 11. HQL:The Hibernate Query Languagehttps://www.hibernate.org/hib_docs/nhibernate/html/queryhql.htmlNHibernate in Actionhttp://www.manning.com/kuate/Reference14