Hibernate:带有非空列的可选多对一
我在两个类之间有一个可选的多对一关系。 Hibernate 通过将外键设置为 null 将该属性转换为可选属性。
我的数据库模式不允许列为空。可选属性由这些列的默认值表示。
<class name="sth.Alpha" ...>
....
<many-to-one name="beta" not-found="ignore" class="sth.Beta" insert="true" update="true">
<column name="a1/>
<column name="a2/>
</many-to-one>
</class>
<class name="sth.Alpha" ...>
<composite-id>
<key-property name="b1" type="int">
<column name="b1" precision="8" scale="0"/>
</key-property>
<key-property name="b2" type="int">
<column name="b2" precision="8" scale="0"/>
</key-property>
</composite-id>
</class>
选择数据没有问题,因为 may-to-one-tag 中的 not-found="ignore"
会导致 null
-beta-对象。但如果我想插入一个
Alpha
呢?将 beta
设置为 null
。我收到异常,无法将 null
插入到 a1
和 a2
中。
如果我将 insert
和 update
设置为 false,我就可以摆脱这个问题。但这会导致在设置后不会保存关系。
数据库架构无法更改,Hibernate 版本固定为 3.5
如果您告诉我这是不可能的,我也会很高兴
I've got an optional many-to-one relationship between two classes. Hibernate translates the property to be optional by setting the foreign keys to null.
My db-schema does not allow the columns to be null. The property to be optional is represented by the default-value of these columns.
<class name="sth.Alpha" ...>
....
<many-to-one name="beta" not-found="ignore" class="sth.Beta" insert="true" update="true">
<column name="a1/>
<column name="a2/>
</many-to-one>
</class>
<class name="sth.Alpha" ...>
<composite-id>
<key-property name="b1" type="int">
<column name="b1" precision="8" scale="0"/>
</key-property>
<key-property name="b2" type="int">
<column name="b2" precision="8" scale="0"/>
</key-property>
</composite-id>
</class>
selecting data is no problem because of not-found="ignore"
in the may-to-one-tag it will result in a null
-beta
-object. But if I want to insert an Alpha
? with beta
set to null
. I get an Exception, that it is not possible to insert null
to a1
and a2
.
I get rid of that problem if I set insert
and update
to false. But this results in not saving the relationship if it is set.
Database-Schema cannot be changed and Hibernate-version is fixed to 3.5
I would also be happy if you tell me, that it is not possible
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如何使用 0 而不是 null 与
结合使用可能会有所帮助或
其他解决方案
how to use 0 instead of null in conjunction with
<id unsavedvalue="whatever">
might helpor
other solution