Hibernate mapping file equivalent of referencedCol

2019-09-02 02:01发布

Does anyone know the hibernate mapping file equivalent of the referencedColumnName property of a @JoinColumns annotation, something like this:

@ManyToOne
@JoinColumns ({
    @JoinColumn(name="FIELD_0", referencedColumnName="A", insertable=false, updateable=false),
    @JoinColumn(name="FIELD_1", referencedColumnName="B", insertable=false, updateable=false),
    @JoinColumn(name="FIELD_2", referencedColumnName="C", insertable=false, updateable=false),
    @JoinColumn(name="FIELD_3", referencedColumnName="D", insertable=false, updateable=false)

Moving to annotations is not an option and we need to setup a foreign key reference with a composite key where column names are different between tables.

Thanks in advance.

1条回答
Luminary・发光体
2楼-- · 2019-09-02 02:15

I've been working on this same problem. I saw your question before I solved it, figured I would come back here and help you. I'm using 4.2.2.

I genericized the names, but I think you will get the point. The "parent" class contains the collection of "child" objects, which means the "child" table has a foreign key constraint to the "parent" table.

Pay close attention to the following: 1. The order of the columns in the collection keys must match the order in the composite-id. 2. The attributes on the bag collection are really important. What I have below is working for me.

<class name="ParentClass" table="parent_table">
    <composite-id name="parentCompositeId" class="ParentCompositeId">
        <key-property name="pkName1" column="pk_name_1" type="integer"/> 
        <key-property name="pkName2" column="pk_name_2" type="string"/>
        <generator class="assigned" />
    </composite-id>

    <property name="name" column="name" type="string" />
    <!-- etc... -->

    <bag name="childObjects" table="child_table" cascade="all" lazy="false" fetch="select">
        <key not-null="true">
            <column name="fk_child_name_1"/>
            <column name="fk_child_name_2"/>
        </key>
        <one-to-many class="ChildClass"/>
    </bag>
</class>

Also, don't try to do anything with the association in either the "child" class or its mapping config. Just map the non-FK properties and let hibernate take care of the FKs.

查看更多
登录 后发表回答