-->
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: Best way to get the date literal?
PostPosted: Thu Jul 01, 2004 9:10 am 
Senior
Senior

Joined: Sun Oct 26, 2003 5:05 am
Posts: 139
I was wondering what is the best way to get a date literal to send to a hibernate query. I figured out one way to do it like this:

Code:
Calendar calendar = Calendar.getInstance();
calendar.set( 2004, 4, 1 );
java.sql.Date start = new java.sql.Date( calendar.getTime().getTime() );


This is kind of bloated. I was think I could move this to a static utility class, something like this:

Code:
java.sql.Date start = DateUtils.getDate( 2004, 4, 1 );


I also thought I could use the date format objects and just take a date and make the correct string. That's probably what the sql Date is doing anyway. In either case, I'm basically making a utility method for it.

How do you guys handle this?[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 01, 2004 9:26 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
In any case, don't use anything else than query.setDate() - don't create any string literals yourself.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 01, 2004 9:36 am 
Senior
Senior

Joined: Sun Oct 26, 2003 5:05 am
Posts: 139
michael wrote:
In any case, don't use anything else than query.setDate() - don't create any string literals yourself.


Hrmm. So you recommend doing all queries through the callback interface rather than using the simpler executeFind* methods? I'm pretty much using those except for when I need to load collections.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 01, 2004 9:38 am 
Senior
Senior

Joined: Sun Oct 26, 2003 5:05 am
Posts: 139
I meant find() methods on the hibernate template in spring. The execute methods were the callback ones. Sorry


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 01, 2004 9:39 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
You should never ever pass direct user input to the database, which is likely the case if you create your queries using string manipulation. Search Google for "SQL injection".

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 01, 2004 9:44 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You can use find, but don't pass a String literal to it, if you actually have a Date - always use a date object.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 01, 2004 9:56 am 
Senior
Senior

Joined: Sun Oct 26, 2003 5:05 am
Posts: 139
Actually, I wasn't passing the string literal (I know not to do that!), I was just using the toString on java.sql.date because setDate or new Object[] { myDate } wasn't quite working, but I found it was something else. I'm actually getting back a date that is one month in advance on that calendar object. So I specify 2004/7/1 and I get back 2004/8/1, where 8 is the month.

I guess I should just rephrase my question then, because clearly I don't quite know the best way to go about this. I have two dates specified in user input and I'd like to make 2 date objects, which I can then pass as query parameters (via query or just in an object array). What is the best way going about that?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 02, 2004 1:06 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
If the "month off" happens from javascript, thats a stupid, brandead, assinine, retarded bug in javascript's Date object (do I sound bitter :). If your saying it happens on constructing a java.util.Date instance, I've never seen that.

In general, I've found its best to parse date strings coming from the UI through use of a java.util.text.DateFormatter using a standard format pattern.

Regardless how you build them, once you have the Date instances, use them in HQL like this:
Code:
Date date1 = ...;
Date date2 = ...;
Query query = session.createQuery("from MyObject as myObj where myObj.startDate between :rangeStartDate and :rangeEndDate");
query.setDate("rangeStartDate", date1);
query.setDate("rangeEndDate", date2);
...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 02, 2004 5:28 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Beware, the month in the calendar.set() method is zero-based, eg

Code:
calendar.set( 2004, 4, 30 );


means 2004-05-30 as a date. I hate that, too.


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.