避免通过JPA向数据库表插入'null'值 [英] Avoid insert 'null' values to database table via JPA

查看:539
本文介绍了避免通过JPA向数据库表插入'null'值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从JPA2开始,到目前为止感觉相当舒适。但是,当使用默认值为NON NULL数据库字段保留具有空属性值的实体时,我遇到了问题。

im starting with JPA2 and feel quite comfortbale so far. But I have a problem when persisting Entities with null property values for NON NULL database fields with default value.

我希望能够将实体属性保留为null并让数据库插入默认值。

I would like to be able to leave the entity property null and let the database insert the default value.

我当前的设置是带有PostgreSQL的openJPA。

My current setup is openJPA with PostgreSQL.

我有这个VERSION数据库table(Vorgabewert =默认值):

I have this VERSION database table (Vorgabewert = Default value):



     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配置。

I have an entity (Java DTO) which maps the database fields (except 'status') by xml configuration.

我希望我可以在没有 system_time 的情况下插入一个实体,并期望数据库将当前时间填充为默认值。

I hoped I could insert an entity without the system_time set and expected that the database will fill the current time as default value.

JPA构造以下SQL查询:

JPA constructs the following SQL-Query:

INSERT INTO public.version (version, activationtime, importtime, system_time, time_) VALUES (?, ?, ?, ?, ?) [params=?, ?, ?, ?, ?]

和Postgres作出反应:

and Postgres reacts with:

FEHLER:Spalte中的NULL-Wert»system_time«verletzt Not-Null-Constraint (对不起德语,但此消息表示'system_time'上的Not-Null-Constraint违规。)

FEHLER: NULL-Wert in Spalte »system_time« verletzt Not-Null-Constraint (sorry for German language but this message means the Not-Null-Constraint violation on 'system_time').

那我该怎么办?这是JPA还是数据库问题。
我可以配置JPA以从INSERT SQL语句中排除空属性。

So what can I do? Is this a JPA or Database Problem. Can I configure JPA to exclude null properties from the INSERT SQL Statement.

我希望能够在我的实体中设置'system_time'或者让它为'null'并让数据库设置默认值。

I want to have the ability to set the 'system_time' in my entity or to let it be 'null' and let the database put the default value.

欢迎任何帮助!

注册
Klaus

Regads Klaus

推荐答案

我不会将数据库中的默认值与JPA结合使用。您必须在插入后读取实体,否则您将在实体状态和db状态之间不匹配。

I would not rely on default values in the database in conjunction with JPA. You would have to read the entity back after the insert otherwise you have a mismatch between the entity state and the db state.

在此处选择实用方法并初始化java中的所有值。从来没有听说过告诉JPA / Hibernate在插入/更新中遗漏空值的方法。

Choose the pragmatic approach here and initialise all values in java. Never heard of a way to tell JPA/Hibernate to leave out null values in an insert/update.

这篇关于避免通过JPA向数据库表插入'null'值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆