Persisting Joda DateTime instead of Java Date in Hibernate

前端 未结 4 1817
一整个雨季
一整个雨季 2021-02-05 13:22

My entities currently contain java Date properties. I\'m starting to use Joda Time for date manipulation and calculations quite frequently. This means that I\'m constantly hav

4条回答
  •  难免孤独
    2021-02-05 13:55

    Dates shouldn't be stored using a high level of abstraction like, say, a string ("2009-08-07 07:43:19 ...") nor as Java objects. They should be persisted as milliseconds since the epoch. Both Joda time and the regular Java date time can give you the time in milliseconds since the epoch. Store the number of milliseconds elapsed and convert back to objects when you're reading back from your DB.

    Persisting heavyweight Date objects instead of a long is a bit like using floating point numbers to represent monetary amounts: it's typically a huge code smell.

提交回复
热议问题