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: Mapping a String to a timestamp?
PostPosted: Mon Dec 04, 2006 6:35 pm 
Newbie

Joined: Mon Dec 04, 2006 5:01 pm
Posts: 5
I'm stuck trying to implement database integration on legacy objects that will not have much added to them and will have even less change in terms of already existing fields. The initial authors decided that to keep size small(this is on an embedded system), all date and time related fields of my objects are going to be stored as Strings. This was fine and dandy for what they wanted to do, but if I want to do date comparison in the database I'm in a spot of trouble. I like timestamps, and my life will be a great deal easier if I can somehow make the strings into a timestamp without affecting my objects. It seems to me that there should be some way to have the String in my object be written as a timestamp to the database, but in all my reading and work with Hibernate I've never come across an issue like this. I've done some research on custom types, and the Hibernate book showed me things that are slightly similar, but I'm still leaning towards the "it's impossible to do this and I'd better start figuring out a workaround" routine, Are there any good guides related to custom types, or has anyone had a similar issue in their interaction with Hibernate?

For what it matters, this will be the latest Hibernate on Postgres. We'lle always use the latest Hibernate, but I'm not sure about us sticking with Postgres in the future. I'm not picky at all in terms of what sort of sql timestamp I'd get.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 04, 2006 9:11 pm 
Senior
Senior

Joined: Sun Jun 04, 2006 1:58 am
Posts: 136
what is the issue that you are facing with implementing
usertypes
http://www.hibernate.org/hib_docs/v3/api/org/hibernate/usertype/UserType.html

_________________
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 05, 2006 11:47 am 
Newbie

Joined: Mon Dec 04, 2006 5:01 pm
Posts: 5
Thanks for the reply scarface. I appreciate it :)

The issue I'm coming across is that I'm not exactly sure I would go about implementing the interface between the String and the SQL timestamp with a CompositeUserType. Hibernate In Action used examples with monetary values in the advanced mapping concepts chapter, but that's moreso converting from one integer to another integer(). I'm looking for a few more examples to help me understand the concept. The old book had one well documented case, but I still don't full understand what exactly I should be doing and I know that a few more examples would help me out.

I'm leaning much more towards a CompositeUserType than a UserType because I'm hoping to have HQL related queries based on time, but I can't really change the String fields in the old code. Does anyone know of any other resources or examples that I might check out? Does the newer version of the book cover this in a bit more detail(or at least give a new example)?

Edit: Oh, a thing I forgot to mention that had confused me in the Hibernate In Action was that they created an immutable MonetaryType class that they mapped with. I was hoping to use a muteable String and didn't have much to compare with in terms of what would change.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 05, 2006 4:56 pm 
Senior
Senior

Joined: Sun Jun 04, 2006 1:58 am
Posts: 136
Not sure i understand your problem completly...

you might want to download the test cases that come with hibernate distribution and check out

org.hibernate.test.legacy.DoubleStringType.java

org.hibernate.test.legacy.MultiplicityType.java

And i dont think even the new version has any other examples ( not that i know of )..even the new version has the monetaryAmount example

_________________
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 5:15 pm 
Newbie

Joined: Mon Dec 04, 2006 5:01 pm
Posts: 5
Part of you not understanding it was me not understanding it either :)

I now have a compositeusertype which handles getting the String in my object to be a timestamp in the database. I want to work with the field(let's call it Apples) as a Timestamp in my HQL queries. Is this possible?

Example:
Suppose this is my class

public class Apples{
...
String DateString;
...
}

for this Apples class, I made a CompositeUserType:

public class ApplesCompositeUserType{
...
// a few interesting tidbits from that class
public Class returnedClass() {
return String.class;
}

public Object nullSafeGet(...){
Date blah = ResultSet.getDate(names[0])
return blah.toString();
} // nullSafeSet will build the Date from a string
...
}

My mapping would be like:
<property name="DateString"
column="TIMESTAMPHERE"
type="ApplesCompositeUserType">

I haven't been able to do a great deal of testing on it yet. What I'm wondering is, what sort of HQL functionality will I be able to use if I run with this sort of a setup? Can I use the HQL date stufflike comparing it to CURRENT_DATE? Can I call something like YEAR(apple.DateString)?

What about making a parameter of this usertype be a timestamp field. If I gave the String a TimeStamp field could I work with that field as if it were a Java Date object?


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.