【发布时间】:2011-05-29 08:39:40
【问题描述】:
我从 JPA2 开始,到目前为止感觉很舒服。但是在为具有默认值的 NON NULL 数据库字段保留具有 null 属性值的实体时,我遇到了问题。
我希望能够将实体属性保留为空并让数据库插入默认值。
我当前的设置是带有 PostgreSQL 的 openJPA。
我有这个 VERSION 数据库表(Vorgabewert = 默认值):
Spalte | Typ | Attribute
----------------+-----------------------------+----------------------------
status_ | smallint | not null Vorgabewert 0
time_ | timestamp without time zone | not null
system_time | timestamp without time zone | not null Vorgabewert now()
version | character varying(20) | not null
activationtime | timestamp without time zone |
importtime | timestamp without time zone |
我有一个实体 (Java DTO),它通过 xml 配置映射数据库字段(“状态”除外)。
我希望我可以插入一个没有设置system_time 的实体,并希望数据库将当前时间填充为默认值。
JPA 构造以下 SQL 查询:
INSERT INTO public.version (version, activationtime, importtime, system_time, time_) VALUES (?, ?, ?, ?, ?) [params=?, ?, ?, ?, ?]
Postgres 的反应是:
FEHLER: NULL-Wert in Spalte »system_time« verletzt Not-Null-Constraint(对不起德语,但此消息表示“system_time”上的 Not-Null-Constraint 违规)。
那我该怎么办?这是 JPA 还是数据库问题。 我可以将 JPA 配置为从 INSERT SQL 语句中排除空属性吗?
我希望能够在我的实体中设置“system_time”或让它为“null”并让数据库放置默认值。
欢迎任何帮助!
问候 克劳斯
【问题讨论】:
标签: java postgresql orm jpa jpa-2.0