繁体   English   中英

不同嵌入式中的相同字段

[英]Same fields in different Embedded

我有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;
}

我有一个不变的实体

@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;
}

我使用EntityManager.createNativeQuery和以下查询创建的代码:

select * from A natural join B natural join C;

但我有一个例外,原因是:

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

更新: 解决方案

向bModel和cModel添加以下注释可以解决此问题:

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

您正在将@ Id,@ Column与可嵌入的类AModel一起使用。 如果要将AModel用作复合键,则可以使用@EmbeddedId。 当您在AEntity中定义@Column(name = a_id)以及在可嵌入类AModel中定义相同的对象时,问题就来了,因此获取重复的逻辑映射。

暂无
暂无

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

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