Hibernate多对一映射具有不同的列数

时间:2022-11-30 16:57:08

Hi I have 2 tables as below

嗨我有2张桌子如下

Table1:

表格1:


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

Primary Key (ID and LOB)

主键(ID和LOB)

Table2:

表2:


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

Primary Key (SK)

主键(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.

我需要从表2到table1给出多对一关系,因为table1具有compositePrimaryKey(ID和LOB),但table2没有任何与LOB相关的列。我无法提供映射。请帮忙。

EDIT I have tried hibernate mapping for Table2:

编辑我已经尝试过Table2的hibernate映射:

<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 is not working. While fetching a record it tries to fetch LOB code from table2 which is not at all exist in Table1

以上不起作用。在获取记录时,它尝试从table2获取LOB代码,这在Table1中根本不存在

3 个解决方案

#1


2  

Assuming table2.SK is a FK to table1.ID and there are no table1 entries having the same ID, you could write the mapping as follows:

假设table2.SK是table1.ID的FK,并且没有具有相同ID的table1条目,您可以按如下方式编写映射:

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

If there are more table1 rows with the same ID, the mapping will fail because a Child would then be matched to multiple Parents.

如果有更多具有相同ID的table1行,则映射将失败,因为Child将与多个Parents匹配。

So, for a proper many-to-one association you need a FK to a Parent column that's unique.

因此,对于正确的多对一关联,您需要FK到父列,这是唯一的。

#2


0  

Hibernate @Id does not have to correspond to the real database primary key (although it is desirable that they match, of course).

Hibernate @Id不必与真正的数据库主键相对应(当然,它们最好匹配)。

If ID is a unique column in Table1, then map Hibernate id only to it, and leave LOB as just an ordinary field.

如果ID是Table1中的唯一列,则只将Hibernate id映射到它,并将LOB保留为普通字段。

If ID is not a unique column, then your many-to-one will not work properly anyway, because there would be multiple matching rows in the referenced table.

如果ID不是唯一列,那么无论如何你的多对一都无法正常工作,因为引用的表中会有多个匹配的行。

#3


-1  

@Entity
@Table(name="Table_name")
public class table_name {
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name = "id1", column = @Column(name = "col1")),
@AttributeOverride(name = "id2", column = @Column(name = "col2")) })

#1


2  

Assuming table2.SK is a FK to table1.ID and there are no table1 entries having the same ID, you could write the mapping as follows:

假设table2.SK是table1.ID的FK,并且没有具有相同ID的table1条目,您可以按如下方式编写映射:

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

If there are more table1 rows with the same ID, the mapping will fail because a Child would then be matched to multiple Parents.

如果有更多具有相同ID的table1行,则映射将失败,因为Child将与多个Parents匹配。

So, for a proper many-to-one association you need a FK to a Parent column that's unique.

因此,对于正确的多对一关联,您需要FK到父列,这是唯一的。

#2


0  

Hibernate @Id does not have to correspond to the real database primary key (although it is desirable that they match, of course).

Hibernate @Id不必与真正的数据库主键相对应(当然,它们最好匹配)。

If ID is a unique column in Table1, then map Hibernate id only to it, and leave LOB as just an ordinary field.

如果ID是Table1中的唯一列,则只将Hibernate id映射到它,并将LOB保留为普通字段。

If ID is not a unique column, then your many-to-one will not work properly anyway, because there would be multiple matching rows in the referenced table.

如果ID不是唯一列,那么无论如何你的多对一都无法正常工作,因为引用的表中会有多个匹配的行。

#3


-1  

@Entity
@Table(name="Table_name")
public class table_name {
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name = "id1", column = @Column(name = "col1")),
@AttributeOverride(name = "id2", column = @Column(name = "col2")) })