-->
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.  [ 3 posts ] 
Author Message
 Post subject: is null on user type
PostPosted: Thu Feb 21, 2013 1:23 pm 
Newbie

Joined: Thu Feb 21, 2013 12:30 pm
Posts: 2
We have a legacy db where all dates are stored as integers and nulls represented by zeros. We have user types that models the integer in database as Joda time, and it can handle translation of nulls into zero through the user type. However, for queries, I'm not sure what we'd have to do to translate the "is null" into "=0" when it goes from HQL to SQL. Is there a way to specify this behavior either in the user type, or the dialect class or elsewhere?


Top
 Profile  
 
 Post subject: Re: is null on user type
PostPosted: Thu Feb 21, 2013 2:24 pm 
Newbie

Joined: Thu Feb 21, 2013 12:30 pm
Posts: 2
So this is what I ended up doing in the user type:

define a
public static final DateTime ZERO_DATE_TIME = new DateTime(0);

and translate that as zero when converting to database:

public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session)
throws HibernateException, SQLException {
if (value == null || value == ZERO_DATE_TIME) {
st.setInt(index, 0);
} else {
doInstanceCheck(value);
DateTime cal = (DateTime) value;
st.setInt(index, getDate(cal));
}
}

So in the query is just set to this special date and it seems to work.


Top
 Profile  
 
 Post subject: Re: is null on user type
PostPosted: Tue Feb 26, 2013 3:46 am 
Beginner
Beginner

Joined: Wed Feb 06, 2013 2:43 am
Posts: 46
Well. It was awesome the way you solved it.

_________________
Thanks,
Ajit Singh
ajits@mindfiresolutions.com
www.mindfiresolutions.com


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