简体   繁体   中英

Same fields in different Embedded

I have 3 tables 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:

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:

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

You are using @Id,@Column with your embeddable class AModel. If you want to make AModel as your composite key then you can use @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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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