Thomas Koch
Thomas Koch

Reputation: 2931

How to use JodaTime Instant (timestamp) with Hibernate 4.x (via Jadira usertype)?

I have a timestamp(0) without time zone column in Postgres and want to map it to a Joda-Time Instant via Hibernate 4.2.2. The Joda-Time site refers to the Jadira UserType library for Hibernate versions 4.x. But there is no documentation.

Can somebody please give me an example of a timestamp Entity property used with Hibernate 4.x, preferable JPA compatible? Is there a possibility to define the mapping once so that I do not need to annotate each timestamp property with a very long class name?

I do not care about timezones. I only persist unix timestamps to the database. Timezones are handled by the View layer.

Upvotes: 2

Views: 4695

Answers (1)

Chris Pheby
Chris Pheby

Reputation: 323

Thomas, as a Starting Point visit this page in the documentation: http://jadira.sourceforge.net/usertype-userguide.html

This is the example there:

@Column @Type(type="org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime") private LocalDateTime dateTime;

Actually that is pretty much all you need to do.

You probably want the PersistentTimestamp class (refer to the Javadoc at http://jadira.sourceforge.net/apidocs/index.html for the full list of classes). The available persistent classes for Joda Time are:

PersistentDateMidnight
PersistentDateMidnightAsString
PersistentDateTime
PersistentDateTimeAndZone
PersistentDateTimeAndZoneWithOffset
PersistentDateTimeAsString
PersistentDateTimeWithZone
PersistentDateTimeZoneAsString
PersistentDateTimeZoneWithOffsetAsString
PersistentDurationAsString
PersistentInstantAsMillisLong
PersistentInstantAsNanosBigInteger
PersistentInstantAsString
PersistentInstantAsTimestamp
PersistentInterval
PersistentLocalDate
PersistentLocalDateAsString
PersistentLocalDateTime
PersistentLocalDateTimeAsString
PersistentLocalTime
PersistentLocalTimeAsMillisInteger
PersistentLocalTimeAsNanosLong
PersistentLocalTimeAsString
PersistentLocalTimeAsTimestamp
PersistentMinutes
PersistentMonthDayAsString
PersistentPeriodAsString
PersistentTimeOfDay
PersistentTimeOfDayAsMillisInteger
PersistentTimeOfDayAsNanosLong
PersistentTimeOfDayAsString
PersistentTimeOfDayAsTimestamp
PersistentYearMonthAsString
PersistentYearMonthDay
PersistentYearMonthDayAsString
PersistentYears

I think you want PersistentInstantAsTimestamp if you are not interested in timezones, or maybe PersistentLocalDateTime.

If have any issues above and beyond this you can follow up here or report them via http://jadira.atlassian.net/ and I will do my best to assist.

Disclaimer: I am the maintainer of Jadira Usertype.

Upvotes: 4

Related Questions