• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hibernate - how to modify value before Hibernate stores it?

 
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Can you hear that? That's my theme music. I don't know where it comes from. Check under this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic