Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hibernate Criteria-based join: no left/right outer?

 
Juan Rolando Prieur-Reza
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would someone confirm or clarify to me whether Hibernate supports the use of Criteria and Projections to perform left outer joins (and right outer joins) ? It appears to only support outer joins unless you use HQL.
Thanks
 
Ankur Jain
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by juan prieur:
Would someone confirm or clarify to me whether Hibernate supports the use of Criteria and Projections to perform left outer joins (and right outer joins) ? It appears to only support outer joins unless you use HQL.
Thanks


Multiple join Criteria Queries in Hibernate
I wanted to use Hibernate's Criteria API to construct a query against an object with two foreign keys that pointed to two separate tables. I couldn't find any examples of how to join all three tables together using Criteria. I've pasted one below for posterity:

Example ex = Example.create(item).ignoreCase().enableLike(MatchMode.EXACT).excludeZeroes();
//Count full list size
Criteria c = session.createCriteria(Item.class).add(ex);
if(item.getRetailer() != null){
c = c.createAlias("retailer","retailer").add(Expression.eq("retailer.id",item.getRetailer().getId()));
}
if(item.getType() != null){
c = c.createAlias("type","type").add(Expression.eq("type.id",item.getType().getId()));
}
List resultsCount = c.setProjection(Projections.projectionList().add(Projections.rowCount())).list();
listWrapper.setFullListSize((Integer)resultsCount.get(0));

//Return requested page
c.setProjection(null);
c.setResultTransformer(Criteria.ROOT_ENTITY);
c = c.setFirstResult( (pageNum-1)*listPageSize);
c = c.setMaxResults(listPageSize);
if( sortOrder == null || !sortOrder.toLowerCase().startsWith("desc") ){
c = c.addOrder( Order.asc(sortColumn) );
}else{
c = c.addOrder( Order.desc(sortColumn) );
}
listWrapper.setSortCriterion(sortColumn);
listWrapper.setSortDirection(sortOrder);
return c.list();
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic