-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Please help from HQL to Criteria
PostPosted: Tue Aug 07, 2007 12:05 am 
Newbie

Joined: Mon Aug 06, 2007 11:58 pm
Posts: 4
query="select distinct p from User as p " +
"JOIN fetch p.Office o JOIN fetch p.UserRoles r JOIN fetch r.SystemRole s " +
"where " +
"lower(p.staffNo) LIKE :staffNo " +
"AND lower(p.badgeNo) LIKE :badgeNo " +
"AND p.Office.officeId = :officeid "+
"AND s.roleId = :roleid "+
"AND ( lower(p.lastName) LIKE :name "+
"OR lower(p.firstName) LIKE :name) "+
"order by o.officeName"


I tried many many days but cannot convert this HQL to Criteria.

Please help ! thx


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 07, 2007 4:52 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
This was as close as I could get:
Code:
      Criteria c = s.createCriteria(User.class)
      .createAlias("office", "o")
      .add(Restrictions.ilike("staffNo", "%staffNo%"))
      .add(Restrictions.ilike("badgeNo", "%badgeNo%"))
      .add(Restrictions.or(
            Restrictions.ilike("firstName", "%name%"),
            Restrictions.ilike("lastName", "%name%")))
      .createCriteria("userRoles")
      .createCriteria("systemRole")
      .add(Restrictions.eq("roleId", new Long(1)))
      .add(Restrictions.eq("o.officeId", new Long(1)))
      .addOrder(Order.asc("o.officeName"))
      .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
      ;


Differences:
- distinct applied in java instead of in the database
- collections lazy populated BUT will contain all items


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 07, 2007 9:39 am 
Newbie

Joined: Mon Aug 06, 2007 11:58 pm
Posts: 4
However, if i use Criteria.DISTINCT_ROOT_ENTITY),

it causes error when i use these for paging purpose
criteria.setFirstResult(offset);
criteria.setMaxResults(size);

any idea? thx

thatmikewilliams wrote:
This was as close as I could get:
Code:
      Criteria c = s.createCriteria(User.class)
      .createAlias("office", "o")
      .add(Restrictions.ilike("staffNo", "%staffNo%"))
      .add(Restrictions.ilike("badgeNo", "%badgeNo%"))
      .add(Restrictions.or(
            Restrictions.ilike("firstName", "%name%"),
            Restrictions.ilike("lastName", "%name%")))
      .createCriteria("userRoles")
      .createCriteria("systemRole")
      .add(Restrictions.eq("roleId", new Long(1)))
      .add(Restrictions.eq("o.officeId", new Long(1)))
      .addOrder(Order.asc("o.officeName"))
      .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
      ;


Differences:
- distinct applied in java instead of in the database
- collections lazy populated BUT will contain all items


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.