Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate - how to modify value before Hibernate stores it?

 
Mike London
Ranch Hand
Posts: 1227
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a situation where I need to store dates in "gmt" format or possibly other locales.

My question is...would the best place to modify how Hibernate would store a field (a date format in this case) be in the setter for the mapped class?

(Since there's no SQL to modify, I'm not sure how to do this.)

Thanks for any suggestions!!!

-- Mike
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you storing a date with a data type that requires you maintain the format? Could you not just use a Date (or Timestamp) type?
 
Mike London
Ranch Hand
Posts: 1227
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is actually a TimeStamp field (my mistake in the original posting).

Does that help the situation to modify the format?

Thanks.

Mike
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you need to store a value in a format that is not supported by hibernate, then you can use custom types. we wrote a custom type to store a MonetaryAmount (amount + currency). A type is basically a translator between ResultSet and Object (when reading from DB) or Object and PreparedStatement (when writing to DB)

create your own Type: http://www.hibernate.org/hib_docs/v3/api/org/hibernate/type/Type.html
mention this type in the mapping: http://www.hibernate.org/hib_docs/v3/reference/en/html/mapping.html#mapping-types
check the caveatemptor hibernate sampleapplication, i think it includes MonetaryAmountUserType class


pascal
 
Mike London
Ranch Hand
Posts: 1227
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe I'm mising the point?

I don't think I need a custom type.

If I need to store a Timestamp value in, say, GMT format, does Hibernate support this simply via the .hbm.xml mapping file?

What I'm asking is how to "get a hold" of the Timestamp value the user enters and makes sure it's stored as GMT by Hibernate -- either via the mapping file or a setter.

Thanks.

Mike
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, misunderstood you there.... though you need to store it in a custom format. did not read properly ;-)


im still not sure i understand your problem though .... i dont think it's hibernate which decides about the format. this is your DB/JDBC driver ? hibernate just uses the setXXX() methods of PreparedStatement.

pascal
 
Mike London
Ranch Hand
Posts: 1227
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you've answered my question, right?

If I need to modify the date format, I can just do it in the setter code before Hibernate gets a hold of it, right?

Mike
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic