开发者

In Java, with JPA2, what class should I use for persisting date information?

There are three options that I see for handling dates in Java, but I may be missing some better alternatives yet.

  1. java.util.Date - Which is fine, but is a very simple class with many design flaws
  2. java.util.Calendar - Which is a bit better, but is still really clumbsy to work with
  3. JODA Time - Which is better to work with, but I'm not sure how we开发者_如何学Pythonll it will persist using JPA2

I would like to use JODA Time because it promises an easier API, but persistence is more important so I'd be fine dealing with Calendar or Date if JODA Time does not persist well.

Ultimately, I need something that persists to a database as a date, preferably with an easy to use API.


We're having hibernate use JODA's LocalDate directly. Of course there's some configuration and whatnot to do. You can look at this project: Joda-Time Hibernate


java.util.Calendar persists fine with JPA2 as well as java.util.Date, just make sure that you add the @Temporal annotation to the property. Persisting JODA times is better supported in Hibernate, however I don't think it supports temporal querying optimizations included in some relational databases so if you plan on querying by the date sticking with java.util objects will be your best bet. If you don't plan on querying on that column any serializable object can be persisted with JPA.


Use java.util.Date for the persisted POJO property, then convert it to Joda Time as an accessor as necessary.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜