我有一個使用Postgresql(具有9.0-801.jdbc3 JDBC驅動程序)的JPA 2應用程序(使用Hibernate 3.6作爲JPA實現)。如何在JPA 2實體中映射postgresql「timestamp with time zone」
我在將「timestamp with time zone」字段映射到我的JPA實體時遇到問題。
下面是一個例子:
CREATE TABLE theme
(
id serial NOT NULL,
# Fields that are not material to the question have been edited out
run_from timestamp with time zone NOT NULL,
run_to timestamp with time zone NOT NULL,
CONSTRAINT theme_pkey PRIMARY KEY (id),
CONSTRAINT theme_name_key UNIQUE (name)
)
我試圖映射如下:
@Entity
@Table(schema = "content", name = "theme")
public class Theme extends AbstractBaseEntity {
private static final long serialVersionUID = 1L;
@Column(name = "run_from")
@NotNull
@Temporal(TemporalType.TIMESTAMP)
private Date runFrom;
@Column(name = "run_to")
@NotNull
@Temporal(TemporalType.TIMESTAMP)
private Date runTo;
/* The rest of the entity has been edited out */
我不斷獲取具有以下根本原因的異常:Caused by: org.hibernate.HibernateException: Wrong column type in public.backend_themetopic for column created. Found: timestamptz, expected: date
什麼我試過
- 與
java.util.Date
更換java.util.Calendar
- 由使用java.sql.Timestamp
沒有區別 - - 抱怨說我不能
@Temporal
標註適用於使用org.joda.time.DateTime
與自定義@Type
註解(@Type(type="org.joda.time.contrib.hibernate.PersistentDateTimeTZ")
)一Timestamp
- 也沒有工作
限制條件
- 該應用程序與「遺留系統」互動 - 因此,改變類型的日期字段是不是一個好的選擇
我的問題是:我應該如何映射這些時區意識到時間戳到我的JPA實體?
這是隻對DDL自動或運行過程中實際有用的事情? – cslotty
'columnDefinition'值只用於DDL。但是,如果您的表列是使用該DDL創建的,則它也會間接影響運行時行爲的具體情況。 –