【问题标题】:Associate set mapping against not primary key column将集合映射与非主键列关联
【发布时间】:2020-11-11 14:36:29
【问题描述】:

我有两个对象,例如:

public class Object1 implements Serializable {
    
    protected Integer id; // This is the PK in the xml mapping

    protected Integer otherId; 
    
}

public class Object2 implements Serializable {
    
    protected Integer id; // This is the PK in the xml mapping
    
    protected Set<Object1> object1List; // I want to relate this set against the "otherId" attribute

}

我有这个休眠 XML 映射:

<class name="Object1" table="Object1Table">
    
    <id name="id" type="java.lang.Integer">
        <column name="id" />
        <generator class="identity" />
    </id>
    
    <property name="otherId" type="java.lang.Integer">
        <column name="other_id"></column>
    </property>
</class>

<class name="Object2" table="Object2Table">
    
    <id name="id" type="java.lang.Integer">
        <column name="id" />
        <generator class="identity" />
    </id>
    
    <set name="object1List" table="Object1Table" lazy="false" fetch="join" >

        <key column="other_id" /> // This is not working

        <one-to-many class="Object1" />
    </set>

</class>

但我不知道如何将“set”映射与“other_id”属性相关联,它仅适用于“Object1”表的 PK“id”。

有谁知道如何解决这种情况?

【问题讨论】:

  • 哇。没有意识到人们仍在使用 XML 进行实体映射。我相信column 属性是指连接列的名称。 one-to-many 上没有 property-ref 属性吗?

标签: java hibernate hibernate-mapping


【解决方案1】:

我认为您需要将&lt;set 标记为inverse。就像提到的 crizzis 一样,如果您使用注释模型,您会更好。更多的人可以帮助您使用注释模型。除此之外,HBM XML 映射将在某个时候被 JPA orm.xml 映射替换,所以我不会再使用这种方法了。

【讨论】:

    猜你喜欢
    • 2019-09-23
    • 2020-07-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-28
    • 1970-01-01
    • 1970-01-01
    • 2011-01-09
    • 1970-01-01
    相关资源
    最近更新 更多