在Java中,使用JPA2,我應該使用哪個類來保存日期信息?

[英]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.

在Java中處理日期有三種選擇,但是我可能會錯過一些更好的選擇。

  1. java.util.Date - Which is fine, but is a very simple class with many design flaws
  2. java.util。Date -這很好,但是是一個非常簡單的類,有很多設計缺陷
  3. java.util.Calendar - Which is a bit better, but is still really clumbsy to work with
  4. java.util。日歷-這更好一點,但仍然是真正的痛
  5. JODA Time - Which is better to work with, but I'm not sure how well it will persist using JPA2
  6. JODA Time——這是更好的工作方式,但是我不確定使用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.

我希望使用JODA Time,因為它可以提供更簡單的API,但是持久性更重要,所以如果JODA Time不能很好地持久,我可以處理日歷或日期。

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

最終,我需要一個作為日期保存在數據庫中的東西,最好是一個易於使用的API。

3 个解决方案

#1


0  

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.

java.util。在JPA2和java.util中,日歷一直保持良好狀態。Date,只需確保將@Temporal注釋添加到屬性。在Hibernate中更支持持久化JODA times,但是我認為它不支持在一些關系數據庫中包含的臨時查詢優化,所以如果您打算按堅持使用java的日期進行查詢的話。util對象將是你最好的選擇。如果您不打算在該列上查詢,任何可序列化的對象都可以使用JPA持久化。

#2


1  

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

我們讓hibernate直接使用JODA的LocalDate。當然,還有一些配置和其他功能。您可以查看這個項目:Joda-Time Hibernate

#3


0  

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

使用java.util。持久化POJO屬性的日期,然后根據需要將其轉換為Joda Time作為訪問器。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2011/01/06/72ef85108813f4932f40d6fa247a15f3.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com