-->
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.  [ 8 posts ] 
Author Message
 Post subject: Bitwise comparison in HQL 3?
PostPosted: Sun Apr 10, 2005 9:53 pm 
Newbie

Joined: Sun Apr 10, 2005 9:35 pm
Posts: 14
Hibernate version: 3

Name and version of the database you are using: MySQL 4.x

Debug level Hibernate log excerpt:
org.springframework.orm.hibernate3.HibernateQueryException: unexpected char: '&' [from uk.co.mindfruit.cms.domain.PlayerGroup as pg where pg.groupType & :listType > 0 order by pg.listOrder]; nested exception is org.hibernate.QueryException: unexpected char: '&' [from com.domain.PlayerGroup as pg where pg.groupType & :listType > 0 order by pg.listOrder]

Hi there,

I have recently upgraded from Hibernate 2.x to Hibernate 3, and Spring 1.1.x to Spring 1.2. I am using Tomcat over a MySQL DB on Windows.

I have an HQL query which used to work under Hibernate 2.x which performs a bitwise comparision of a stored integer with a parameter. My question is how I can work around the fact that the '&' operator appears no longer to be valid HQL. I don't particularly want to go to native SQL, nor do I really want to create separate columns to store boolean values (the bits of the stored integer act as flags).

The HQL looks like this:

Code:
     select positiongroups, players
       from PositionGroup as positiongroups
       join positiongroups.positions.players as players
       join players.groups as groups
      where groups.id = :groupId 
        and positiongroups.groupType & 4 > 0
   order by positiongroups.listOrder"


Any suggestions would be very welcome. I hope I've posted enough information for people: there was no stacktrace of any great value and the mapping document wouldn't be relevant.

Cheers

Mike


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 11, 2005 11:37 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Add a SQLFunction to the dialect, that maps to the bitwise operator.


Top
 Profile  
 
 Post subject: Sample code
PostPosted: Mon Apr 18, 2005 3:55 pm 
Newbie

Joined: Sun Apr 10, 2005 9:35 pm
Posts: 14
Hi there,

Thanks for the tip, Gavin, I've got it working now after having some trouble realising the Hql parser wouldn't merely let me substitute a function name for the offending '&' character. I had to pass both the SQL column name and the comparison value as arguments to my new SQLFunction.

Code:
/**
* @author Michael Guyver
*/
public class MySQLDialect extends org.hibernate.dialect.MySQLDialect {

   public MySQLDialect() {
       super();
       registerFunction("bitwise_and", new MySQLBitwiseAndSQLFunction("bitwise_and", Hibernate.INTEGER));
   }

}


Code:
import java.util.List;
import org.hibernate.dialect.function.SQLFunction;
import org.hibernate.dialect.function.StandardSQLFunction;
import org.hibernate.type.Type;

/**
* @author Michael Guyver
*/
public class MySQLBitwiseAndSQLFunction extends StandardSQLFunction
implements SQLFunction {

   public MySQLBitwiseAndSQLFunction(String name) {
       super(name);
   }

   public MySQLBitwiseAndSQLFunction(String name, Type typeValue) {
       super(name, typeValue);
   }

   public String render(List args) {
       if (args.size() != 2){
           throw new IllegalArgumentException("the function must be passed 2 arguments");
       }
       StringBuffer buffer = new StringBuffer(args.get(0).toString());
       buffer.append(" & ").append(args.get(1));
       return buffer.toString();
   }

}


Once you've instructed Hibernate to use your new dialect you could write the following HQL, where 'listType' is a parameter:
Code:
from PlayerGroup as pg where bitwise_and('pg.groupType', :listType) > 0 order by pg.listOrder


This should generate some SQL which includes:
Code:
... where pg.groupType & 3 > 0 order by ...


where the parameter value was 3. Hope this helps anyone with a similar dilemma.

Mike

kenevel@hotmail.com


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 18, 2005 5:03 pm 
Newbie

Joined: Mon Apr 18, 2005 4:47 pm
Posts: 4
Location: Rugby, UK
Thanks for posting a complete example of how to do this Mike - I was just about to ask for the very same!

Hibernate Team - are there plans to reintroduce this to the HQL parser to remain consistent with the behaviour of the Hibernate 2 parser?

Darren


Top
 Profile  
 
 Post subject: Whoops... my mistake
PostPosted: Mon Apr 18, 2005 5:36 pm 
Newbie

Joined: Sun Apr 10, 2005 9:35 pm
Posts: 14
Hi there,

Just to update the above HQL sample, you should not quote the SQL column you pass to the bitwise_and function. It should read
Code:
where bitwise_and(pg.groupType, :listType)

not
Code:
where bitwise_and('pg.groupType', :listType)

Sorry about that...

Mike


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 14, 2005 1:33 pm 
Newbie

Joined: Mon Apr 18, 2005 4:47 pm
Posts: 4
Location: Rugby, UK
Hi,

I implemented the above workaround when first migrating to Hibernate 3. It was working fine until I recently upgraded to Hibernate 3.0.2 and then 3.0.3 at which point it stopped working - the custom SQLFunction just does not seem to be called.

I also raised this in a new topic:

http://forum.hibernate.org/viewtopic.php?t=942418

Has anyone seen this problem, and if so, what is the workaround?

Thanks,

Darren


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 21, 2008 11:41 am 
Beginner
Beginner

Joined: Wed Sep 07, 2005 9:57 am
Posts: 20
The SQLFunction interface changed:

Code:
   public String render(List args, SessionFactoryImplementor factory throws QueryException {
       if (args.size() != 2){
           throw new IllegalArgumentException("the function must be passed 2 arguments");
       }
       StringBuffer buffer = new StringBuffer(args.get(0).toString());
       buffer.append(" & ").append(args.get(1));
       return buffer.toString();
   }


Top
 Profile  
 
 Post subject: Re: Bitwise comparison in HQL 3?
PostPosted: Wed Aug 05, 2009 3:24 am 
Newbie

Joined: Wed Aug 05, 2009 3:19 am
Posts: 2
Hello.
I`m using 4 dialects in my work. Is there any way to add bitwise_and to all of them at a time, or should I create 4 classes to extend dialects and 1 to add the bitwise_and function?


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