您好,我有如下 2 个表格
表 1:
+-------------------+ | ID LOB col1 col2 | +-------------------+
Primary Key (ID and LOB)
Table2:
+-----------------+ | SK ID col3 col4 | +-----------------+
Primary Key (SK)
I need to give a many to one relation from table 2 to table1, since table1 has compositePrimaryKey(ID and LOB) but table2 does not have any column related to LOB. I am unable to provide the Mapping. Please help on this.
EDIT I have tried hibernate mapping for Table2:
<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>
以上是行不通的。在获取记录时,它会尝试从表 2 中获取 LOB 代码,而表 1 中根本不存在该代码
最佳答案
假设 table2.SK
是 table1.ID
的 FK,并且没有 table1
条目具有相同的 ID,您可以编写映射如下:
@ManyToOne
@JoinColumn(name = "ID", insertable = false, updatable = false)
private Class1 class1Obj;
如果 table1
中有多个具有相同 ID 的行,映射将失败,因为一个 Child 将被匹配到多个 Parents。
因此,对于正确的多对一
关联,您需要 FK 到唯一的 Parent 列。
关于java - 具有不同列数的 Hibernate 多对一映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31477128/