-->
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: SQLProjection in MSSQL/MYSQL - date diff
PostPosted: Sun Mar 19, 2006 9:03 am 
Newbie

Joined: Tue Jun 14, 2005 12:44 pm
Posts: 19
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:
MySql 4.1, MSSQL
The generated SQL (show_sql=true):
select *, Ended - Started as duration from sessions this_


I need to calculate the duration between two dates (Ended - Started) and have them return in a seperate column. I chose to do this via an SQLProjection:

Code:
criteria.setProjection( Projections.projectionList()
.add(Projections.alias(Projections.sqlProjection("Ended - Started as duration",new String[] {"duration"},new org.hibernate.type.Type[]{Hibernate.TIMESTAMP}),"duration"))


The problem is - this code works well on MSSQL, however on MySQL I receive a ClassCast exception - apparently subtracting dates in MySQL returns a FLOAT.

My question is - how can I make my code work for both databases without reducing it to switch-case clauses? I want to make it as generic as possible.

PS I thought of using DATEDIFF in the SQLProjeciton but the syntax is again different for the two databases.

10x2all


Top
 Profile  
 
 Post subject: OBJECT
PostPosted: Mon Mar 20, 2006 6:50 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
Perhaps you could use Hibernate.OBJECT as type and then in the code check the actual type of returned object and then do necessary calculations and transformations.

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


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.