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.  [ 4 posts ] 
Author Message
 Post subject: Dynamically Creating Criteria Join Queries W/o Using Aliases
PostPosted: Thu Sep 23, 2010 1:57 am 
Newbie

Joined: Fri Sep 17, 2010 9:19 am
Posts: 2
Hi,
I am new to Hibernate and currently having a problem with creating search query dynamically using Criteria API.
I want to know whether we can write multi-join queries in criteria without using aliases, as its really difficult to
dynamically to identify the field and apply alias against it to be able to access it in projections list.

Thanks...in advance.......

Sumit Joglekar.


Top
 Profile  
 
 Post subject: Re: Dynamically Creating Criteria Join Queries W/o Using Aliases
PostPosted: Thu Sep 23, 2010 4:57 am 
Newbie

Joined: Thu Sep 23, 2010 4:43 am
Posts: 10
Hi,

You cannot write multi-join queries using Criteria API, (if you do not want to use reflection to find entity field.)
i suggest you to use, org.hibernate.SQLQuery class to map generic conditions, you can set fields using index values.

sqlQuery.setParameter(0,X);
sqlQuery.setParameter(1,Y);
..


Top
 Profile  
 
 Post subject: Re: Dynamically Creating Criteria Join Queries W/o Using Aliases
PostPosted: Mon Nov 15, 2010 11:06 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
Here is my Criteria based multi join query. It works fine, except that I would like it to return only the mail instances and not the admin ones. I guess I need some projections to do that...

Code:
   public List<Mail> findLikePattern(String pattern) {
      pattern = "%" + pattern + "%";
      Criterion description = Restrictions.ilike("description", pattern);
      Criterion subject = Restrictions.ilike("subject", pattern);
      Criterion body = Restrictions.ilike("body", pattern);
      Criterion firstname = Restrictions.ilike("a.firstname", pattern);
      Criterion lastname = Restrictions.ilike("a.lastname", pattern);
      Criterion login = Restrictions.ilike("a.login", pattern);
      Criterion email = Restrictions.ilike("a.email", pattern);
      Criterion name = Restrictions.ilike("c.name", pattern);
      Disjunction disjunction = Restrictions.disjunction();
      disjunction.add(description).add(subject).add(body).add(firstname).add(lastname).add(login).add(email).add(name);
      Criteria criteria = getSession().createCriteria(Mail.class);
      criteria.add(disjunction).addOrder(Order.asc("subject")).createAlias("admin", "a", Criteria.LEFT_JOIN).createAlias("mailCategory", "c", Criteria.LEFT_JOIN);
      return criteria.list();
   }


Top
 Profile  
 
 Post subject: Re: Dynamically Creating Criteria Join Queries W/o Using Aliases
PostPosted: Wed Nov 24, 2010 11:52 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
Anyone on this projection ?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.