-->
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: need criteria/hql for TimeStamp-Query
PostPosted: Mon Nov 13, 2006 5:55 am 
Regular
Regular

Joined: Mon Jul 31, 2006 4:59 pm
Posts: 53
Hibernate Version 3.2

i am using MySql and i want to save "java.sql.Timestamp". How can i read all elements with criteria or hql, if for example the year must be "2006" ?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 13, 2006 6:42 am 
Newbie

Joined: Thu Nov 10, 2005 6:05 am
Posts: 16
you can use the "year(...)" function in both HQL and Criteria.

in HQL:

session.createQuery("from Foo where year(_date) = :y").setParameter("y", 2006).list();

in Criteria API:

...
add(Restrictions.sqlRestriction("year(_date) = ?", 2006, Hibernate.INTEGER)
...

see more in
http://www.hibernate.org/hib_docs/v3/re ... teria.html
http://www.hibernate.org/hib_docs/v3/re ... ryhql.html

regards, Ivan


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 14, 2006 8:39 am 
Regular
Regular

Joined: Mon Jul 31, 2006 4:59 pm
Posts: 53
Thank you for your answer. Sorry, but it doesnt work. My java.sql.Timestamp is for example: "2006,10,14,15,34,00"

when i try to execute this query:

Code:
List result = session.createQuery("from MyTable mt where year(mt.mydate) = :year").setParameter("year", String.valueOf(year)).list();


if get an java.lang.ClassCastException . Any idea whats wrong?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 14, 2006 11:10 am 
Newbie

Joined: Thu Nov 10, 2005 6:05 am
Posts: 16
2 things:

what are the commas in your timestamp? or is that only a special format (is this Oct 14, 2006, 15:34 ?

why do you convert the year param to String? String.valueOf(year)? The query expects an integer, maybe that's a reason of the classcast exception. If I understand year is an integer (2006), you should leave it as is:

Code:
List result = session.createQuery("from MyTable mt where year(mt.mydate) = :year").setParameter("year", year).list();


anyway what is the stacktrace?

ivan


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 14, 2006 11:16 am 
Regular
Regular

Joined: Mon Jul 31, 2006 4:59 pm
Posts: 53
That with the year must be an error, there are dots.

I have to convert it to a String, because setParameter doesnt accept an integer.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 14, 2006 11:44 am 
Newbie

Joined: Thu Nov 10, 2005 6:05 am
Posts: 16
well, it accepts in my code...
try with Integer.valueOf(year)

ivan


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.