-->
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.  [ 7 posts ] 
Author Message
 Post subject: How to store a DB Integer as Java Date??
PostPosted: Tue Feb 06, 2007 11:16 am 
Newbie

Joined: Tue Feb 06, 2007 11:02 am
Posts: 5
The database I am connecting to, stores their dates as Integers in the format: 20060523 (yyyyMMdd). I would like to be able to store these fields as java.util.Date objects within my persistence objects. In this application I am only using hibernate to read from the database (never to write to).

The conversion is easy to code with:
new SimpleDateFormat("yyyyMMdd").parse(integerDate.toString())

I tried doing the conversion within the getter/setter, but I notice these methods don't get used when populating the object from the database.

Anyone know how I can do this??... or another other idea which would result in getting these dates within the my persistence object as a Date object??


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 06, 2007 1:45 pm 
Newbie

Joined: Mon Sep 29, 2003 3:32 am
Posts: 6
1. The getter/setter should be used IMHO, maybe you should look for "lazy"
definitions in your mapping and play with "lazy=false" ?

2. Another simple way is using a "UserType". Just tage a look at the
documentation for examples. That way your conversion code goes into the
UserType-Definition and your java property becomes a Date.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 06, 2007 2:23 pm 
Newbie

Joined: Mon Dec 18, 2006 2:39 am
Posts: 16
Hmm i would be interested to see how that conversion was implemented in the getter/setters... I would think it should work fine there. Could you post that snippet of code?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 06, 2007 2:36 pm 
Newbie

Joined: Tue Feb 06, 2007 11:02 am
Posts: 5
Here is what I tried:

/** persistent field */
private Date ulexpiry; //term expiry date

/**
* @hibernate.property
* column="ULEXPIRY"
* length="10"
* not-null="true"
*
*/
public Date getUlexpiry() {
return this.ulexpiry;
}

public void setUlexpiry(Integer ulexpiry) {
this.ulexpiry = new SimpleDateFormat("yyyyMMdd").parse(ulexpiry.toString());
}

When I query the database however to populat this persistence object, it doesn't care about the setter, it just complains about the mismatch of trying to set the db integer to a java Date.

(FYI, I am new to hibernate... so if there is something obvious I'm missing, it may not be obvious to me)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 06, 2007 4:05 pm 
Newbie

Joined: Tue Feb 06, 2007 11:02 am
Posts: 5
Talk about the perfect find:

Here a link which not only tells me how to do this with a User Type, but their example is exactly what I'm doing to the tee.

http://www.i-proving.ca/space/Technolog ... +Hibernate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 06, 2007 4:09 pm 
Newbie

Joined: Mon Dec 18, 2006 2:39 am
Posts: 16
heh nice find....I was going to say that it might be thrownig that exception because in that setter method you are setting an integer to Date, it may compile fine, but i'm guessing the database doesn't like it.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 06, 2007 4:31 pm 
Newbie

Joined: Tue Feb 06, 2007 11:02 am
Posts: 5
Very funny coincidence.

I know why that example fit so perfectly for me. I recognized the company in the package name of the example. I look at the author and I recognize the name as someone from a software consulting company who worked downstairs from me. So I open up the source code to the project the author was working on here, and find the code from the example.

Small world.


In reading the further examples in the article, I can also see other problems I'll have with this dirty legacy db :)


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