1

I am new with hibernate. I have an table in my DB (mySQL) with "startTime" column. It's type is time.
I downloaded org.joda.time package and use Period type, but this doesn't work and throws exception: 10954 [main] ERROR org.hibernate.property.BasicPropertyAccessor - expected type: org.joda.time.Period, actual value: java.sql.Time.

Which type should be the member in my java code?

Naor
  • 23,465
  • 48
  • 152
  • 268
  • 1
    I had to do this with joda-time and hibernate 4 recently, here is how http://stackoverflow.com/a/8989253/106261 – NimChimpsky Feb 03 '12 at 17:10

4 Answers4

1

you should always store date and time in UTC in database. java.util.Date is what you should use as it represents the date and time in UTC and hibernate supports it.

Related post How to store date/time and timestamps in UTC time zone with JPA and Hibernate

If you only want to use joda-time then there are already converters written. You should use Joda time - Hibernate support library.

Community
  • 1
  • 1
Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327
  • he wants to use joda time in tandem with hibernate so your suggestion would defeat the whole purpose – MozenRath Feb 03 '12 at 16:53
  • @MozenRath there is no real need to use Joda here – Aravind Yarram Feb 03 '12 at 16:54
  • @MozenRath: I don't really want to use joda time since I just prefer to use the most suitable type for time. – Naor Feb 03 '12 at 16:56
  • I prefer to use joda-time, java date/calendar is an abomination – NimChimpsky Feb 03 '12 at 17:11
  • @NimChimpsky it is for data manioulations but it is not required for this simple case. – Aravind Yarram Feb 03 '12 at 17:14
  • ohhk. I thought since you use joda time, you would want to handle all data as per joda time API – MozenRath Feb 03 '12 at 20:07
  • FYI, the [*Joda-Time*](http://www.joda.org/joda-time/) project is now in [maintenance mode](https://en.wikipedia.org/wiki/Maintenance_mode), advising migration to the [*java.time*](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Sep 24 '19 at 02:32
1

Hibernate manages natively java.util.Date (and subclasses like java.sql.Date).

In order to use Joda Time object in your object model you can use the Joda provided Hibernate types for Hibernate up to version 3.6, and the Usertype project for Hibernate 4.

Stefano Travelli
  • 1,889
  • 1
  • 15
  • 18
0

You will have to write a converter from java.sql.time to jodatime period and use it in your mapping. This happens because in the database it will always store value as java.sql.Time

MozenRath
  • 9,652
  • 13
  • 61
  • 104
0

I have used LocalDateTime from joda package, and I have used "org.joda.time.contrib.hibernate.PersistentLocalDateTime" as the custom type in hibernate, and it works. Am not sure , if this is what you are using.

Sajan Chandran
  • 11,287
  • 3
  • 29
  • 38