-->
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.  [ 5 posts ] 
Author Message
 Post subject: Calculate difference between 2 dates
PostPosted: Mon Jul 18, 2005 6:27 am 
Newbie

Joined: Mon Jul 18, 2005 5:44 am
Posts: 3
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

[b]Hibernate version:[/b]
Hibernate Synchronizer
[b]Mapping documents:[/b]
hibernate-mapping-2.0.dtd
[b]Name and version of the database you are using:[/b]
DB2

dear all,
i need to calculate the date range between 2 date , my hql goes like this:

select '2005-07-18 00:00:00.0'-AL.DtDocumentDate
from tableA as AL where AL.SiRecordStatus = 1
and AL.DtDocumentDate <= '2005-07-18 00:00:00.0'
and AL.BiDebtor =11 and AL.ChClass='I'

and i got the error message like following :

net.sf.hibernate.QueryException: undefined alias: '2005-07-18 00:00:00 [select '2005-07-18 00:00:00.0'-AL.DtDocumentDate from com.ifca.mcorp.hh.ArdmxLedger as AL where AL.SiRecordStatus = 1 and AL.DtDocumentDate <= '2005-07-18 00:00:00.0' and AL.BiDebtor =11 and AL.ChClass='I']

- hibernate just didnt regconised the date . when i select date out from database , is in timestamp format , so i try to use timestamp for my given date .. but result is the same ...

hope can get any help from u all.
tans ! :)


Top
 Profile  
 
 Post subject: Possible solution
PostPosted: Mon Jul 18, 2005 10:00 pm 
Regular
Regular

Joined: Wed May 11, 2005 11:57 pm
Posts: 80
This worked for me:

Code:
   public void testDateProjection() throws HibernateException {
      Session session = TestUtils.sessionFactory.openSession();
      List list = session.createQuery( "select student.enrollmentDate from Student student where enrollmentDate <= '2005-07-18 0:00:00' ").list();
      assertTrue( list.get(0) instanceof Date );
   }


I don't think you're supposed to have the actual literal date value as one of the fields to select.

--Jesse


Top
 Profile  
 
 Post subject: Tans for reply
PostPosted: Tue Jul 19, 2005 8:39 pm 
Newbie

Joined: Mon Jul 18, 2005 5:44 am
Posts: 3
hi, tans for ur reply .
yeah , i think it is not possible for me to get the difference by my above hql . so , as alternative way , i just select out the date and then do my calculation myself.
anyway , tans for ur reply .
:)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 19, 2005 9:39 pm 
Newbie

Joined: Wed May 18, 2005 3:02 pm
Posts: 18
I'm pretty new to Hibernate and HQL, but most databases have various date/time manipulation functions built in, including something like DATE_SUB(). If those built in functions are available from within HQL, then you can use them to do your subtraction.

--sam


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 20, 2005 11:05 am 
Newbie

Joined: Tue Jul 19, 2005 6:39 am
Posts: 4
Location: Venice, Italy
Hi,
my opinion is: let the database do its work - the data persistance - and let the code do its work - the processing -.
In other words, even if from a SQL point of view it is correct, I wouldn't write such a query in Hibernate.
I think you should add some methods for date calculations - such as add, substract, days, etc. - or to the class containing the date property or, in a more general way, to a user type extending Date.
Cheers
Adriano


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