Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Query Session - IV
Different ways to express queries in Hibernate <ul><li>Hibernate Query Language (HQL) </li></ul><ul><li>Criteria API for  ...
HQL <ul><li>Sample HQL Query </li></ul><ul><li>Query qry = session.createQuery(&quot;from Message&quot;); </li></ul><ul><l...
Bind Parameters in HQL <ul><li>Named parameters: </li></ul><ul><li>String queryString = &quot;from Item item where item.de...
Executing a Query <ul><li>Listing all results </li></ul><ul><li>Ex: </li></ul><ul><li>Query qry = session.createQuery(&quo...
Named Query <ul><li>Mapping Details: </li></ul><ul><li><hibernate-mapping> </li></ul><ul><li><sql-query name=&quot;getByNa...
Joins in HQL <ul><li>An  implicit  association join </li></ul><ul><li>EX: </li></ul><ul><li>  from Item i where i.bids.amo...
Marshalling Scalar Resultset <ul><li>Normal Query </li></ul><ul><li>select i.id,i.name,b.amount from Item i join i.bids b ...
Criteria <ul><li>Sample Criteria </li></ul><ul><li>User user = (User) session.createCriteria(User.class) </li></ul><ul><li...
Few Criteria samples <ul><li>Sample1:- </li></ul><ul><li>List result = session.createCriteria(Item.class) </li></ul><ul><l...
<ul><li>Sample5:- </li></ul><ul><li>session.createCriteria(Item.class) </li></ul><ul><li>.add( Restrictions.gt(&quot;endDa...
Example <ul><li>Sample Example </li></ul><ul><li>Item item = new Item(); </li></ul><ul><li>item.setName(&quot;TV&quot;); <...
Native SQL Queries <ul><li>Auto Marshalling Sample1:- </li></ul><ul><li>List result = session.createSQLQuery(&quot;select ...
Upcoming SlideShare
Loading in …5
×

Hibernate Session 4

2,585 views

Published on

Published in: Technology, Sports
  • Be the first to like this

Hibernate Session 4

  1. 1. Query Session - IV
  2. 2. Different ways to express queries in Hibernate <ul><li>Hibernate Query Language (HQL) </li></ul><ul><li>Criteria API for query by criteria (QBC) </li></ul><ul><li>query by example (QBE) </li></ul><ul><li>NativeSql using Hibernate API </li></ul>
  3. 3. HQL <ul><li>Sample HQL Query </li></ul><ul><li>Query qry = session.createQuery(&quot;from Message&quot;); </li></ul><ul><li> List list = qry.list(); </li></ul>
  4. 4. Bind Parameters in HQL <ul><li>Named parameters: </li></ul><ul><li>String queryString = &quot;from Item item where item.description like :search&quot;; </li></ul><ul><li>Query q = session.createQuery(queryString) </li></ul><ul><li> .setString(&quot;search&quot;, searchString); </li></ul><ul><li>Positional parameters: </li></ul><ul><li>String queryString = &quot;from Item item where item.description like ?&quot;; </li></ul><ul><li>Query q = session.createQuery(queryString) </li></ul><ul><li> .setString(0, searchString); </li></ul>
  5. 5. Executing a Query <ul><li>Listing all results </li></ul><ul><li>Ex: </li></ul><ul><li>Query qry = session.createQuery(&quot;from Item&quot;); </li></ul><ul><li>List list = qry.list(); </li></ul><ul><li>Iterating through the results </li></ul><ul><li>Ex: </li></ul><ul><li>Query qry = session.createQuery(&quot;from Item &quot;); </li></ul><ul><li>Iterator it = qry.iterate(); </li></ul><ul><li>Scrolling with database cursors </li></ul><ul><li>Ex: </li></ul><ul><li>Query qry = session.createQuery(&quot;from Item &quot;); </li></ul><ul><li>ScrollableResults scrolRS = qry.scroll(); </li></ul>
  6. 6. Named Query <ul><li>Mapping Details: </li></ul><ul><li><hibernate-mapping> </li></ul><ul><li><sql-query name=&quot;getByName&quot;> </li></ul><ul><li><return alias=&quot;m&quot; class=&quot;hello.Message&quot;/> </li></ul><ul><li>SELECT {m.*} FROM MESSAGES m WHERE m.MSG_TEXT = ? </li></ul><ul><li></sql-query> </li></ul><ul><li></hibernate-mapping> </li></ul><ul><li>Usage: </li></ul><ul><li>Query qry = session.getNamedQuery(&quot;getByName&quot;); </li></ul><ul><li>qry.setParameter(0, &quot;Kathir Text&quot;); </li></ul><ul><li>List msgList = qry.list(); </li></ul>
  7. 7. Joins in HQL <ul><li>An implicit association join </li></ul><ul><li>EX: </li></ul><ul><li> from Item i where i.bids.amount > 100 </li></ul><ul><li>An ordinary join in the FROM clause </li></ul><ul><li>EX: </li></ul><ul><li> from Item i join i.bids b where i.name = 'aa' and b.amount >50 </li></ul><ul><li>A fetch join in the FROM clause </li></ul><ul><li>EX: </li></ul><ul><li> from Item i left join fetch i.bids </li></ul><ul><li>A theta-style join in the WHERE clause </li></ul><ul><li>EX: </li></ul><ul><li> from Item i,Message m where i.name = m.text </li></ul>
  8. 8. Marshalling Scalar Resultset <ul><li>Normal Query </li></ul><ul><li>select i.id,i.name,b.amount from Item i join i.bids b where i.name = 'aa‘ </li></ul><ul><li>It returns collection of object[]. </li></ul><ul><li>Marshalling Scalar Values </li></ul><ul><li>select new ItemBidDetail(i.id,i.name,b.amount) from Item i join i.bids b where i.name = 'aa‘ </li></ul><ul><li>It returns collection of ItemBidDetail. </li></ul>
  9. 9. Criteria <ul><li>Sample Criteria </li></ul><ul><li>User user = (User) session.createCriteria(User.class) </li></ul><ul><li>.add(Restrictions.eq(&quot;email&quot;, &quot;foo@hibernate.org&quot;)) </li></ul><ul><li>.list(); </li></ul>
  10. 10. Few Criteria samples <ul><li>Sample1:- </li></ul><ul><li>List result = session.createCriteria(Item.class) </li></ul><ul><li>.add(Restrictions.like(&quot;description&quot;,&quot;Foo&quot;,MatchMode.ANYWHERE)) </li></ul><ul><li>.createCriteria(&quot;bids&quot;) </li></ul><ul><li>. add( Restrictions.gt(&quot;amount&quot;, new BigDecimal(99) ) ).list(); </li></ul><ul><li>Sample2:- </li></ul><ul><li>session.createCriteria(User.class) </li></ul><ul><li>.add( Restrictions.eq(&quot;username&quot;, &quot;foo&quot;).ignoreCase() ); </li></ul><ul><li>Sample3:- </li></ul><ul><li>session.createCriteria(Item.class) </li></ul><ul><li>.createAlias(&quot;bids&quot;, &quot;b&quot;) </li></ul><ul><li>.add( Restrictions.like(&quot;description&quot;, &quot;%Foo%&quot;) ) </li></ul><ul><li>.add( Restrictions.gt(&quot;b.amount&quot;, new BigDecimal(99) ) ); </li></ul><ul><li>Sample4:- </li></ul><ul><li>session.createCriteria(Item.class) </li></ul><ul><li>.setFetchMode(&quot;bids&quot;, FetchMode.JOIN) </li></ul><ul><li>.add( Restrictions.like(&quot;description&quot;, &quot;%Foo%&quot;) ); </li></ul>
  11. 11. <ul><li>Sample5:- </li></ul><ul><li>session.createCriteria(Item.class) </li></ul><ul><li>.add( Restrictions.gt(&quot;endDate&quot;, new Date()) ) </li></ul><ul><li>.setProjection( Projections.id() ); </li></ul><ul><li>Sample6:- </li></ul><ul><li>session.createCriteria(Item.class) </li></ul><ul><li>.setProjection( Projections.projectionList() </li></ul><ul><li>.add( Projections.id() ) </li></ul><ul><li>.add( Projections.property(&quot;description&quot;) ) </li></ul><ul><li>.add( Projections.property(&quot;initialPrice&quot;) ) </li></ul><ul><li>); </li></ul><ul><li>Sample7:- </li></ul><ul><li>Criteria cri = session.createCriteria(Bid.class) </li></ul><ul><li> .setProjection(Projections. projectionList () </li></ul><ul><li> .add(Projections. id ().as(&quot;bidId&quot;)) </li></ul><ul><li> .add(Projections. property (&quot;amount&quot;).as(&quot;amount&quot;)) </li></ul><ul><li> ) </li></ul><ul><li>.setResultTransformer(new AliasToBeanResultTransformer(BidDetails.class) </li></ul><ul><li>) </li></ul><ul><li>.addOrder(Order. asc (&quot;amount&quot;)); </li></ul>Few Criteria samples cont..,
  12. 12. Example <ul><li>Sample Example </li></ul><ul><li>Item item = new Item(); </li></ul><ul><li>item.setName(&quot;TV&quot;); </li></ul><ul><li>Criteria cri = session.createCriteria(Item.class) </li></ul><ul><li> .add(Example. create (item)); </li></ul>
  13. 13. Native SQL Queries <ul><li>Auto Marshalling Sample1:- </li></ul><ul><li>List result = session.createSQLQuery(&quot;select * from CATEGORY&quot;) </li></ul><ul><li>.addEntity(Category.class).list(); </li></ul><ul><li>Auto Marshalling Sample2:- </li></ul><ul><li>session.createSQLQuery( </li></ul><ul><li>&quot;select {i.*} from ITEM I join USERS u on i.SELLER_ID = u.USER_ID&quot; + </li></ul><ul><li>&quot; where u.USERNAME = :uname&quot;) </li></ul><ul><li>.addEntity(&quot;i&quot;, Item.class) </li></ul><ul><li>.setParameter(&quot;uname&quot;, &quot;johndoe&quot;); </li></ul>

×