Java 持久化@Column nullable = false 可以插入null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19444214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Persistence @Column nullable = false can insert null
提问by Mathew Rock
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 documentationand I don′t know why doesn′t work.
@Column(name="QWECOD", nullable = false)
private String qwe;
我想做这一列不能为空,但是当我在数据库中插入一个寄存器值时,这允许我插入。我阅读了文档,但不知道为什么不起作用。
@Column(name="QWECOD", nullable = false)
private String qwe;
THX
谢谢
UPDATE: I′m using Toplinkand java org.eclipse.persistence.eclipselink:2.4.2.
更新:我正在使用Toplink和 java org.eclipse.persistence.eclipselink:2.4.2。
采纳答案by Andreas Aumayr
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.
我认为如果您使用实体管理器的实现生成架构,则可以使用 nullable。我不知道它是否/必须在持久化实体的同时进行验证。
Maybe it helps if you use the @NotNull annotation, but this is NOT plain JPA. It's defined in the JSR-303
如果您使用 @NotNull 注释可能会有所帮助,但这不是普通的 JPA。它在JSR-303 中定义
There is a topic on Stackoverflow too that covers your question: Stackoverflow - Confusion notnull vs columnnullable false
Stackoverflow 上也有一个主题涵盖了您的问题: Stackoverflow - Confusion notnull vs columnnullable false
EDIT: In the JPA 2.1 Specification, there is this section:
编辑:在 JPA 2.1 规范中,有这个部分:
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.
11.2.2.1 列
在模式生成中使用列注释的以下元素:
名称
唯一
可为空的 列定义
表
长度(仅限字符串值列)精度(仅限精确数字(十进制/数字)列)比例(精确数字(十进制/数字) ) 仅列) 有关适用于这些元素和列创建的规则,请参阅第 11.1.9 节。AttributeOverride 注释可用于覆盖列映射。
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.
由于没有给出其他提示,我假设以下内容:如果符合 JPA 的 EntityManager 创建模式,它必须使用等效的 DB 相关约束(例如 notnull)在特定列上应用可为空约束,它不是由实体管理器检查,而是由基础数据库检查。因此,如果 DB 引发错误,EntityManager 会将这个错误传播给调用者。
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.
如果您在不使用 DB 可为空约束的情况下自己创建表,则实体管理器会尝试保留实体并不会出现错误 --> 保留是可以的,尽管存在一些不应该存在的空值。
回答by Pankaj Sharma
if you directly do insert in database then Hibernate or any persistence provider would not ne able to control you. try to insert using persistence provider.
如果您直接在数据库中插入,则 Hibernate 或任何持久性提供程序将无法控制您。尝试使用持久性提供程序插入。