Hibernate的saveOrUpdate忽略非空列属性 [英] Hibernate's saveOrUpdate ignores not-null column property
问题描述
我有一个映射文件,它将给定的列指定为 not-null =true
。这是一个错误,因为表的列在Oracle数据库上设置为 NULL
。但是直到现在我们才注意到,在创建映射文件一年多之后,因为Hibernate一直在忽略这个。这可能吗?
为了更清楚。在数据库中:
CREATE TABLE db.my_table
(...)
my_column NUMBER(10,0)NULL,
(...)
在映射文件上:
< column name =MY_COLUMNprecision =10 scale =0not-null =true>
然后在Java代码上:
getHibernateTemplate()。saveOrUpdate(myEntity);
getHibernateTemplate()。flush();
此代码适用于我们的环境。一直以来。但有些客户端遇到 ot-null属性引用null或transient值
的问题,当我调试代码时,它没有任何意义。据我所知,此代码应该永远无法运行。
当然,解决客户端问题很简单,我只需修改映射文件即可正确代表我的实体。但真正的问题是为什么没有Hibernate抱怨它?
我已经问过一些其他工程师在这里有更多的关于Hibernate的经验,但他们都没有见过所以,任何人都可以给我一个提示吗?
编辑:要强调我们的测试环境和我的客户端都运行完全相同的代码,并且在这两种情况下, myEntity
对象都有 myColumn
属性设置为 NULL
。所以,令我困惑的是为什么它在我们的环境中不会产生任何异常。
not-null
属性有两个含义: - 支持模式导出工具
- 在运行时检查实体(即不检查数据库列设置)
请参阅: 5.1。映射声明,摘录:
映射文档还包含一些额外的可选属性以及影响数据库的
元素
导出工具(例如not-null属性)。
和 5.1.11。属性,摘录:
非空(可选):启用DDL生成因此,如果你的客户端运行一些代码,它会尝试:$ /
$ / b
$ b $ / b
$ bgetHibernateTemplate()。saveOrUpdate(myEntity);
而
myEntity
缺少一些属性集如not-null =true
在抛出运行时异常时是正确的。在您的测试环境中,您很可能总是将属性设置为一些非空值。
甚至有一个优点。数据库和应用程序松散耦合。所以如果需要的话,你可以在应用端进行更多的约束,而不触及数据库(例如你不允许)。
I have a mapping file that specifies a given column as not-null="true"
. This is a mistake since the table's column is set to NULL
on the Oracle database. But we did not notice that until now, more than an year after creating the mapping file, because Hibernate have been "ignoring" this. Is that possible?
To make it clearer. On the database:
CREATE TABLE db.my_table
(...)
my_column NUMBER(10,0) NULL, (...)
On the mapping file:
<column name="MY_COLUMN" precision="10" scale="0" not-null="true">
Then on the Java code:
getHibernateTemplate().saveOrUpdate(myEntity);
getHibernateTemplate().flush();
This code is WORKING on our environment. Has always been. But some clients have had issues of ot-null property references a null or transient value
and when I debugged the code it didn't make any sense. This code should never been able to run, as far as I know.
Of course it's simple to solve the clients problems, I just have to correct the mapping file so it represents my entity properly. But the real issue here is why haven't Hibernate complained about it?
I've asked some other engineers here with more experience on Hibernate but none of them have ever seen this.
So, can anyone gimme a hint?
EDIT: Just want to stress that both our test environment and my client's are running the exact same code, and in both cases the myEntity
object has the myColumn
property set to NULL
. So, what puzzles me is why it does not yield any exception here on our environment.
It is absolutely correct behaviour.
The not-null
attribute has two meanings:
- support schema exporting tools
- check the Entity in runtime (i.e. do not check database column setting)
See: 5.1. Mapping declaration, extract:
The mapping document also contains some extra optional attributes and elements that affect the database schemas exported by the schema export tool (for example, the not-null attribute).
And 5.1.11. Property, extract:
not-null (optional): enables the DDL generation of a nullability constraint for the columns.
So, if your clients run some code, which tries:
getHibernateTemplate().saveOrUpdate(myEntity);
while the myEntity
is missing some property set as not-null="true"
it is correct in throwing a runtime exception. While in your test environment you most likely always set the property to some not null value.
And there is an advantage even. The DB and App are loosely coupled. So if needed, you can make more constraints on App side, while not touching the DB (e.g. you are not allowed)
这篇关于Hibernate的saveOrUpdate忽略非空列属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!