Java – hibernate many to one mapping and different column numbers

Hi, I have two tables as follows

Table 1:

    +-------------------+
    | ID  LOB col1 col2 |
    +-------------------+

Primary key (ID and LOB)

Table 2:

    +-----------------+
    | SK ID col3 col4 |
    +-----------------+

Primary key (SK)

Since table1 has compositeprimarykey (ID and LOB), but table2 does not have any columns related to lob, I need to give multiple relationships from table 2 to table 1 I cannot provide a mapping Please help.

Edit I have tried the hibernate mapping of Table 2:

<many-to-one name="class1Obj" class="com.acs.enterprise.common.Class1" 
            lazy="proxy" insert="false" update="false">
    <column name="ID" />
    <column name="LOB" />
</many-to-one>

The above will not work While getting the record, it tries to get the lob code from table2, which does not exist in table1

Solution

Suppose table2 SK is an FK to table1 ID, and there is no table1 entry with the same ID, you can write the mapping as follows:

@ManyToOne
@JoinColumn(name = "ID",insertable = false,updatable = false)
private Class1 class1Obj;

If more table1 rows have the same ID, the mapping will fail because a child will then be matched to multiple parents

Therefore, for an appropriate many - to - one association, you need an FK to the parent column, which is unique

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>