-->
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.  [ 2 posts ] 
Author Message
 Post subject: problem with obtaining the sum of dates
PostPosted: Tue Aug 22, 2006 6:18 am 
Newbie

Joined: Wed Aug 09, 2006 8:06 am
Posts: 2
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:3.1
Name and version of the database you are using: Sql Server

I need to obtain sum of two date columns (using group by) as the number of days.

In HQL i would use this:

Code:
Query q = session.createQuery( ""
+ "select new map( "
+ " period.username as username,"
+ " period.travelCountry as country,"
+ " SUM(period.endDate-period.startDate)"
+ " )"
+ " from CalendarPeriod period"
+ " where period.endDate >= :startDate"
+ " and period.startDate <= :endDate"
+ " group by period.username, period.travelCountry"
);


Hoverver I cannot obtain the difference between two dates using
SUM(period.endDate-period.startDate)... because of using SqlServer i would use native query (via session.createSqlQuery()) using the SqlServer native date calculation functions:

SUM( DATEDIFF( Day, start_date, end_date ))

but unfortunaltelly i'm not able to produce correct code.. Can anyone lend me a hand and show how to rewrite this using native sql query?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 22, 2006 11:20 am 
Newbie

Joined: Thu May 04, 2006 5:04 am
Posts: 17
Location: Austria
You can execute native SQL Queries using the createSQLQuery Method from the Criteria class.
Its like the PreparedStatement using the ? as a placeholder.

Pseudo code:
SQLQuery sqlQuery = session.createSQLQuery("select ... from ... where x=? and y=?");
sqlQuery.setDate(0, ...);
sqlQuery.setDate(1, ...);
List result = sqlQuery.list();

The objects returned in the result depend on the Columns, iirc a single column is returned as a single appropriate object while more columns are returned as an array of objects in the result.

<Don't forget to rate if this helped you>


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