简体   繁体   English

不同嵌入式中的相同字段

[英]Same fields in different Embedded

I have 3 tables A, B, C: 我有3个表A,B,C:

@Embeddable
public class AModel {
    @Column(name = "a_id", insertable = false, updatable = false)
    private int id;
    @Column(name = "a_name")
    private String name;
}

@Embeddable
public class BModel {
    @Column(name = "b_id", insertable = false, updatable = false)
    private int id;
    private int aId;
    @Column(name = "b_name")
    private String name;
}

@Embeddable
public class CModel {
    @Column(name = "c_id", insertable = false, updatable = false)
    private int id;
    private int aId;
    @Column(name = "c_name")
    private String name;
}

and I have immutable entity 我有一个不变的实体

@Entity
@Immutable
public class AEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "a_id")
    private int id;

    @Embedded
    private AModel aModel;
    @Embedded
    private BModel bModel;
    @Embedded
    private CModel cModel;
}

which I create with EntityManager.createNativeQuery and following query: 我使用EntityManager.createNativeQuery和以下查询创建的代码:

select * from A natural join B natural join C;

But I got an exception caused by: 但我有一个例外,原因是:

org.hibernate.DuplicateMappingException:  Table [aentity] contains physical column name [a_id] represented by different logical column names: [a_id], [aId]

Upd: Solution 更新: 解决方案

Adding following annotation to bModel and cModel can solve this problem: 向bModel和cModel添加以下注释可以解决此问题:

@AttributeOverrides({
            @AttributeOverride(name = "aId", column = @Column(name = "a_id", insertable = false, updatable = false))
    })

You are using @Id,@Column with your embeddable class AModel. 您正在将@ Id,@ Column与可嵌入的类AModel一起使用。 If you want to make AModel as your composite key then you can use @EmbeddedId. 如果要将AModel用作复合键,则可以使用@EmbeddedId。 The issue is coming as you are defining @Column(name = a_id) inside AEntity and also the same thing inside embeddable class AModel so getting duplicate logical mapping. 当您在AEntity中定义@Column(name = a_id)以及在可嵌入类AModel中定义相同的对象时,问题就来了,因此获取重复的逻辑映射。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM