2017-02-28 97 views
1

有誰知道如何修復實體的映射?如何使用@EmbeddedId映射實體?

錯誤是

所致:org.hibernate.AnnotationException:在JoinColumns.referencedColumnName未發現 com.test.TableAa的列名ID。

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; 
} 
+0

格式的代碼,請 – Mistalis

回答

0

在你TAbleAa你應該有你的一對多映射如下:

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

您通過嵌入它的名字是pk引用。

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