【问题标题】:Hibernate: optional many-to-one with not-null columnsHibernate:具有非空列的可选多对一
【发布时间】:2012-01-25 13:25:02
【问题描述】:

我在两个类之间有一个可选的多对一关系。 Hibernate 通过将外键设置为 null 将属性转换为可选。

我的 db-schema 不允许列为空。可选属性由这些列的默认值表示。

<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-object。但是如果我想插入一个Alpha?将beta 设置为null。我收到一个异常,无法将null 插入a1a2

如果我将insertupdate 设置为false,我就可以解决这个问题。但是如果设置了关系,这会导致不保存关系。

Database-Schema 无法更改,Hibernate-version 固定为 3.5

如果你告诉我这是不可能的,我也会很高兴

【问题讨论】:

    标签: java hibernate orm hbm


    【解决方案1】:

    how to use 0 instead of null&lt;id unsavedvalue="whatever"&gt; 结合使用可能会有所帮助

    other solution

    【讨论】:

    • 您好,感谢您的回答,听起来很有希望。但是您要链接的页面至少要关闭几天。但我会尝试 unsavedvalue-property
    猜你喜欢
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    • 2011-02-16
    • 2012-06-14
    • 1970-01-01
    • 1970-01-01
    • 2017-01-03
    • 2013-11-19
    相关资源
    最近更新 更多