-->
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: Hibernate is generating wrong SQL
PostPosted: Fri Aug 28, 2009 9:00 pm 
Newbie

Joined: Tue Jun 16, 2009 4:11 pm
Posts: 8
When I execute the following HQL, hibernate is generating a wrong SQL instruction:

Code:
   1. StringBuffer hql = new StringBuffer(); 
   2. hql.append(" select gau.id                                           "); 
   3. hql.append(" from GroupAndUser as gau                                "); 
   4. hql.append(" ,GroupAuthorization as auth                             ");   
   5. hql.append(" where                                                   "); 
   6. hql.append("  gau.user.id = :userKey                                 "); 
   7. hql.append("  and auth.userGroup = gau.group                         "); 
   8. hql.append("  and (                                                  ");           
   9. hql.append("       (                                                 "); 
  10. hql.append("        auth.classKey = :classKey                        "); 
  11. hql.append("        and auth.permissionString like :permissionString "); 
  12. hql.append("       )                                                 "); 
  13. hql.append("       or gau.group.admin = true                         "); 
  14. hql.append("      )                                                  "); 
  15.   
  16. Session sess = PersistenceUtil.getSession(database); 
  17. sess.beginTransaction(); 
  18.   
  19. try { 
  20.     Query query = sess.createQuery(hql.toString()); 
  21.     query.setParameter("userKey", userKey); 
  22.     query.setParameter("classKey", classKey); 
  23.     query.setParameter("permissionString", "%" + permission + "%"); 
  24.       
  25.     return query.uniqueResult() != null; 
  26. } finally { 
  27.     sess.getTransaction().rollback(); 
  28. } 



and (
groupautho1_.classKey=?
and (
groupautho1_.permissionString like ?
)
or usergroup2_.admin=true
)


Generated SQL:

Code:
select 
     groupandus0_.id as col_0_0_   
from 
     SYSPERMISSION groupandus0_, 
     SYSPERMISSION groupautho1_, 
     SYSUSER usergroup2_   
where 
     groupandus0_.groupid=usergroup2_.id   
     and groupandus0_.userid=?   
     and groupautho1_.groupid=groupandus0_.groupid   
     and ( 
         groupautho1_.classKey=?   
         and ( 
             groupautho1_.permissionString like ? 
         )   
         or usergroup2_.admin=true 
     )


Any idea why Hibernate is generating a wrong where clause? I already tried to switch the order and still gettong the wrong SQL.

Any help would really be appreciatted.
Thanks.


Top
 Profile  
 
 Post subject: Re: Hibernate is generating wrong SQL
PostPosted: Mon Aug 31, 2009 1:24 pm 
Newbie

Joined: Tue Jun 16, 2009 4:11 pm
Posts: 8
Anybody????


Top
 Profile  
 
 Post subject: Re: Hibernate is generating wrong SQL
PostPosted: Mon Aug 31, 2009 2:23 pm 
Regular
Regular

Joined: Mon Jan 05, 2009 6:42 pm
Posts: 99
Location: IL
Hi,
I looked into your query and not sure how/why hibernate would generate that way. So sorry this post is not an answer. But have you tried Restrictions API and try to do the same thing?

Also have you tried something like :-
8. hql.append(" and ( ");
9. hql.append(" ( ");
10. hql.append(" auth.classKey = :classKey ");
11. hql.append(" and auth.permissionString = :permissionStringSingleValue ");
12. hql.append(" ) ");
13. hql.append(" or gau.group.admin = true ");
14. hql.append(" )

Also
8. hql.append(" and ( ");
9. hql.append(" ( ");
10. hql.append(" auth.classKey = :classKey ");
11. hql.append(" and auth.permissionString like :permissionString ");
12. hql.append(" ) ");
13. hql.append(" or gau.group.admin = :booleanValue ");
14. hql.append(" )

or combination of both..
Good Luck!
Srilatha.


Top
 Profile  
 
 Post subject: Re: Hibernate is generating wrong SQL
PostPosted: Mon Aug 31, 2009 2:47 pm 
Newbie

Joined: Tue Jun 16, 2009 4:11 pm
Posts: 8
Hi, I already tried switching the order, replacing the literal for a parameter, but unfortunately nothing worked :(
Could this be a bug, or am i doing something wrong? If it's a bug i'll consider quit using HQL since i have other SQLs much more complex to convert to HQL.
Do you know any good book on HQL?

Thanks for the answer.


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.