如何用@EmbeddedId映射实体?

时间:2017-02-28 11:09:22

标签: java hibernate jpa

有谁知道如何修复实体的映射?

错误是

  

引起:org.hibernate.AnnotationException:列名ID   com.test.TableAa在JoinColumns.referencedColumnName中找不到。

TableAa实体

@Entity
@Table(name = "TABLE_AA")
public class TableAa {
    @EmbeddedId
    private TableAaPk pk;
    @Column(name = "FIRST_NAME")
    private String first_name;
    @Column(name = "LAST_NAME")
    private String last_name;
    //@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    //@JoinColumns({
    //@JoinColumn(name = "ID", referencedColumnName = "ID", insertable = false, updatable = false),
    //@JoinColumn(name = "ACCOUNT_NUMBER", referencedColumnName = "ACCOUNT_NUMBER", insertable = false, updatable = false) })       
    @OneToMany(mappedBy = "tableAa", cascade = CascadeType.ALL)
    private List<TableBb> tableBbList;
}

TableAaPk复合键

@Embeddable
public class TableAaPk implements Serializable{
    private static final long serialVersionUID = 1L;
    @Column(name="ID")
    private String id;
    @Column(name="ACCOUNT_NUMBER")
    private String accountNumber;
}

TableBb实体

@Entity
@Table(name = "TABLE_BB")
public class TableBb {
    @EmbeddedId
    private TableBbPk pk;
    @Column(name = "FIRST_NAME")
    private String first_name;
    @Column(name = "LAST_NAME")
    private String last_name;
}

TableBbPk复合键

@Embeddable
public class TableBbPk implements Serializable{
    private static final long serialVersionUID = 1L;
    @Column(name="ID")
    private String id;
    @Column(name="ACCOUNT_NUMBER")
    private String accountNumber;
    @Column(name="CODE")
    private String code;
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumns({
    @JoinColumn(name = "ID", referencedColumnName = "ID", insertable = false, updatable = false),
    @JoinColumn(name = "ACCOUNT_NUMBER", referencedColumnName = "ACCOUNT_NUMBER", insertable = false, updatable = false) }) 
    private TableAa tableAa;
}

2 个答案:

答案 0 :(得分:0)

在您的TAbleAa中,您应该按照以下方式进行OneToMany映射:

@OneToMany(mappedBy = "pk.tableAa", cascade = CascadeType.ALL)
private List<TableBb> tableBbList;

您通过可嵌入的名称pk引用。

答案 1 :(得分:0)

我认为你在这个实体中的问题

@Entity
@Table(name = "TABLE_BB")
public class TableBb {
    @EmbeddedId
    private TableBbPk pk;
    @Column(name = "FIRST_NAME")
    private String first_name;
    @Column(name = "LAST_NAME")
    private String last_name;

    // I think the solution is to add the relation @ManyToOne which mapped by in the other side
    @ManyToOne // because u refere to this name (tableAa) in @OneToMany(mappedBy = "tableAa"...) in TableAa entity
    private TableAa tableAa;
}