【问题标题】:Mapping a map to the same table as its class in hibernate在休眠中将映射映射到与其类相同的表
【发布时间】:2012-08-09 14:36:49
【问题描述】:

我想映射一个相当简单(我认为)的类:

<class name="parent" table="PARENT_TABLE">
    <composite-id>
        <key-property name = "a" column = "A"/>
        <key-property name = "b" column = "B"/>
        <key-property name = "c" column = "C"/>
    </composite-id>
    <map name = "theMap" table = "PARENT_TABLE" where="type='access'">
        <key foreign-key = "PARENT_TABLE_FK">
            <column name = "A"/>
            <column name = "B"/>
            <column name = "C"/>
        </key>
        <map-key column = "X" type = "double"/>
        <element column = "Y" type = "double" not-null="true"/>
    </map>
</class>

我从mapping multiple sets in one table in hibernate 得到了where="type='access'" 技巧,但问题是在映射中,外键 (A,B,C,X) 与父表 (A,B, C)。

有没有人知道如何让这个快乐?如果我必须单独映射地图,则 PARENT_TABLE 将是完全多余的。

【问题讨论】:

  • 不排队是什么意思? A、B、C 是否不是唯一的,但对于每个 X、Y 组合都是重复的?
  • 如,它希望父项的键与子项的键匹配,因为它是一个映射并且具有额外的键列'X',所以这是不可能的。您是正确的,每个子行都重复了 ABC。我要做的是防止两个表的 ABC 列具有相同的数据。

标签: java hibernate collections map


【解决方案1】:

它并不漂亮,并且还使 Schemaexport 无用,但要使其正常工作,您需要指定 where 条件以使父级唯一,将插入/更新设置为 false 并让映射间接保留父级。

<class name="parent" table="PARENT_TABLE" where="x = (SELECT x FROM PARENT_TABLE p WHERE p.(a,b,c) = a,b,c LIMIT 1)" insert="false" update="false">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-09
    • 2017-11-14
    • 1970-01-01
    相关资源
    最近更新 更多