-->
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.  [ 6 posts ] 
Author Message
 Post subject: How to use Modulo Operation ( 10 % 3 = 1) in Hibernate HQL.
PostPosted: Thu Aug 11, 2005 8:36 am 
Newbie

Joined: Sat Jan 15, 2005 12:47 pm
Posts: 6
Hibernate version: 3.0.5

HI, I have a pretty silly question about Modulo Operation in HQL.
How can I use it in a HQL query.

I think I missed a function Name or operator for this.
My Searching on the Forum and on the Internet had no success in finding any infomration about this topic.
It seems to me that hibernate does not support this Operation in HQL.
Documentation does not even contain the word modulo.
Perhaps it is non Standard, but it is a needed fucntion.

My query looks like:
---------------------------------------------------------------------------------
SELECT a FROM Cat a WHERE (a.age % a.numberOfChilds) > 10
---------------------------------------------------------------------------------

Hibernate Exception:
org.hibernate.QueryException: unexpected char: '%'

I am looking for a way to use this Modulo Operation without Native SQLQuery.
Is there any Solution you know. Maybe a secret operation name, or a way to enhance Hibernate to add this Operation.

Thanks in advance to all of you, who are reading this.
I hope you can help me.


Top
 Profile  
 
 Post subject: Don't use HQL
PostPosted: Thu Aug 11, 2005 8:46 am 
Senior
Senior

Joined: Thu Aug 04, 2005 4:54 am
Posts: 153
Location: Birmingham, UK
If HQL doesn't support modulo then you can always just use a native SQL query. Providing your DB supports it of course, but it is part fo the ANSI-92 standard.


Top
 Profile  
 
 Post subject: I know that, but Hibernate should support this function
PostPosted: Thu Aug 11, 2005 8:54 am 
Newbie

Joined: Sat Jan 15, 2005 12:47 pm
Posts: 6
Hi, as I wrote before I know it is possible to use SQLQuery but I am looking for a way to use HQL for solving this problem.

But thanks for the try.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 11, 2005 12:45 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Create a custom dialect with your own SQLFunction.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 24, 2005 11:47 am 
Newbie

Joined: Sun Nov 07, 2004 7:03 am
Posts: 8
Quick fix, a little dirty but simple:

SELECT a FROM Cat a WHERE (a.age - a.age / a.numberOfChilds * a.numberOfChilds) > 10


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 09, 2006 6:57 pm 
Newbie

Joined: Thu Mar 09, 2006 6:46 pm
Posts: 1
Actually, the SQL posted above works for some databases, but it doesn't work at least for Oracle, because Oracle never does integer division, it always converts to floating point.

Here's some modulo SQL that works in Hibernate and works in all the databases I'm aware of:

a % b =>

select cast((cast(a as real)/cast(b as real) - cast(cast(a as real)/cast(b as real) -0.5 as integer)) * cast(b as real) as integer)


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