持久性@Column nullable = false可以插入null [英] Persistence @Column nullable = false can insert null

查看:289
本文介绍了持久性@Column nullable = false可以插入null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做这个列不能为null,但是当我在数据库中插入一个寄存器值null时,这允许我插入.我阅读了文档,但我不知道为什么不起作用. @Column(name="QWECOD", nullable = false) private String qwe;

THX

更新:我正在使用 Toplink 和java org.eclipse.persistence.eclipselink:2.4.2 .

解决方案

我认为,如果您使用entitymanager的实现生成架构,则会使用nullable.我不知道在持久化实体时是否也必须对其进行验证.

如果使用@NotNull批注可能会有所帮助,但这不是普通的JPA.它在 JSR-303

在Stackoverflow上也有一个主题,涵盖了您的问题: Stackoverflow-混淆notnull与columnnullable false


在JPA 2.1规范中,有以下部分:

11.2.2.1列
架构生成中使用了Column批注的以下元素:
名称
独特
可空
columnDefinition 表
长度(仅字符串值的列) 精度(仅精确的数字(十进制/数字)列) 比例(仅精确的数字(十进制/数字)列) 有关适用于这些元素和列创建的规则,请参见11.1.9节. AttributeOverride批注可用于覆盖列映射.

由于没有其他提示,我假设以下内容: 如果符合JPA的EntityManager创建模式,则它必须通过使用与数据库等效的约束(例如notnull)在特定列上应用可为空的约束. 当您保留一个实体时,Entitymanager BUT不会通过基础数据库对其进行检查.因此,如果数据库引发错误,则EntityManager将此错误传播到调用者.

如果您在不使用DB可为空的约束的情况下自行创建表,则entitymanager会尝试保留该实体,而不会出现错误->持久化还可以,但是有些空值不应该存在.

I want to do this column can´t be null but when I insert in database one register values null this allows me inserted. I read documentation and I don´t know why doesn´t work. @Column(name="QWECOD", nullable = false) private String qwe;

THX

UPDATE: I´m using Toplink and java org.eclipse.persistence.eclipselink:2.4.2.

解决方案

I think nullable is used if you generate the schema, using the implementation of the entitymanager. I don't know whether it is / has to be validated while persisting an entity as well.

Maybe it helps if you use the @NotNull annotation, but this is NOT plain JPA. It's defined in the JSR-303

There is a topic on Stackoverflow too that covers your question: Stackoverflow - Confusion notnull vs columnnullable false


EDIT: In the JPA 2.1 Specification, there is this section:

11.2.2.1 Column
The following elements of the Column annotation are used in schema generation:
name
unique
nullable
columnDefinition table
length (string-valued columns only) precision (exact numeric (decimal/numeric) columns only) scale (exact numeric (decimal/numeric) columns only) See section 11.1.9 for the rules that apply to these elements and column creation. The AttributeOverride annotation may be used to override column mappings.

As there is no other hint given, I assume the following: If a JPA-conform EntityManager CREATES the schema, it HAS to apply the nullable constraint on the specific column by using an aequivalent DB related constraint (e.g. notnull) When you persist an entity, it is NOT checked by the Entitymanager BUT by the underlying databse. So if the DB raises an error, the EntityManager propagates this error up to the caller.

If you create the table yourself without using the DB nullable constraint, then the entitymanager tries to persist the entity and gets NO error --> persist is okay althouh there are some null values that shouldn't be there.

这篇关于持久性@Column nullable = false可以插入null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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