-->
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.  [ 9 posts ] 
Author Message
 Post subject: Hibernate 3 mysql DATE_ADD issue with INTERVAL keyword
PostPosted: Thu Mar 17, 2005 1:45 am 
Beginner
Beginner

Joined: Thu Aug 26, 2004 5:53 am
Posts: 37
Hibernate version: 3.0 rc1

I am trying to do this in HQL :

allotment.date >= DATE_FORMAT(DATE_ADD( :currentDateTime, INTERVAL supplier_leadtimes HOUR ), '%Y-%m-%d')

the parser barfs saying

org.springframework.orm.hibernate3.HibernateQueryException: unexpected token: supplier_leadtimes

I presume this is because of the INTERVAL keyword - and will also be the case with the HOUR keyword.

Is there anything that I can do with the dialect to support this, or does the parser need to be changed ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 17, 2005 1:55 am 
Beginner
Beginner

Joined: Thu Aug 26, 2004 5:53 am
Posts: 37
I should mention that this worked in hibernate 2.1.2


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 17, 2005 2:03 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
You should be able to add a SQLFunctionTemplate to the Dialect.

Make up a HQL function, and have it produce the SQL you want.

Another approach is to define a SQL function in the db.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 17, 2005 2:10 am 
Beginner
Beginner

Joined: Thu Aug 26, 2004 5:53 am
Posts: 37
I can see from

http://cvs.sourceforge.net/viewcvs.py/h ... &view=auto

That I can subclass the Dialect, override the constructor and call

Code:
registerFunction( "date_add_interval", new SQLFunctionTemplate( Hibernate.STRING, "date_add(?1 || INTERVAL ?2 ?3)" ) );


While looking at the mysql dialect, I may have found a bug

Code:
registerFunction( "concat", new SQLFunctionTemplate( Hibernate.STRING, "concat(?1 || ?2)" ) );


shouldn't that be

Code:
registerFunction( "concat", new SQLFunctionTemplate( Hibernate.STRING, "concat(?1, ?2)" ) );


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 17, 2005 2:12 am 
Beginner
Beginner

Joined: Thu Aug 26, 2004 5:53 am
Posts: 37
whoops..

should be

Code:
registerFunction( "date_add_interval", new SQLFunctionTemplate(Hibernate.STRING, "date_add(?1, INTERVAL ?2 ?3)" ) );


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 17, 2005 2:16 am 
Beginner
Beginner

Joined: Thu Aug 26, 2004 5:53 am
Posts: 37
this works

hibernate 3 rocks !


Custom Dialect
Code:
public class Dialect extends MySQLInnoDBDialect {

   public Dialect() {
      super();
      registerFunction( "date_add_interval", new SQLFunctionTemplate( Hibernate.DATE, "date_add(?1, INTERVAL ?2 ?3)" ) );
   }
   
}


Modified HQL
Code:
allotment.date >= DATE_FORMAT( date_add_interval(:currentDateTime, supplier_leadtimes, HOUR), '%Y-%m-%d')


Any chance that something like this can be added to the real mysql dialect ?
[/b]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 17, 2005 2:25 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
I fixed the bug in CVS.

I think it will be too much work to add all these kinds of things to the standard dialects, we just need to document that these extensions are possible.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 17, 2005 2:33 am 
Beginner
Beginner

Joined: Thu Aug 26, 2004 5:53 am
Posts: 37
I can see your point.

Maybe there needs to be a dialect project that provides all of the function templates for each specific version of databases.

That way people can just add an extra jar and use all of the custom db functions out of the box.


Top
 Profile  
 
 Post subject: Still not work.
PostPosted: Sun Oct 30, 2005 10:34 pm 
Newbie

Joined: Mon Sep 05, 2005 9:51 pm
Posts: 3
Hi gavin,

Even in 3.1rc2, date_add function of my sql doesn't work. I used date_add function with hibernate 2.1 and with 3.0.beta4, but in later version hibernate throw exceptions. It may be parser problem.

Currently I'm using date_add_interval with custom dialect, but to my thought i must work correctly without custom dialect.

thanks.


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