避免通过JPA向数据库表插入'null'值

编程入门 行业动态 更新时间:2024-10-25 16:27:46
本文介绍了避免通过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'值

本文发布于:2023-10-25 22:23:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1528279.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库   JPA   null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!