jpa多对多具有附加列和复合键

时间:2016-09-08 17:04:09

标签: java hibernate jpa many-to-many

我有2个表:文件夹(简单主键)和文档(复合主键)
我想要一个名为folder_documents的连接表,它将包含具有附加列的两个表的id

有我的实体:

文件夹

@Entity
@Table(name="document")
public class Document {

    @EmbeddedId 
    private DocumentID documentCompositeKey;

    @Column
    private Date date;

文档

@Embeddable
public class DocumentID implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private String id;
    private String matricule;

DocumentID(复合键)

@Entity
@Table(name = "folder_documents")
@AssociationOverrides({
    @AssociationOverride(name = "folder_documents_compositeKey.folder",
        joinColumns = @JoinColumn(name = "folder_id")),
    @AssociationOverride(name = "folder_documents_compositeKey.document",
        joinColumns = @JoinColumn(name = "doc_id" , referencedColumnName = "id")), // error mapping there
    @AssociationOverride(name = "folder_documents_compositeKey.document",
        joinColumns = @JoinColumn(name = "matricule" , referencedColumnName = "matricule"))})// error mapping there
public class Folder_Documents {

    @EmbeddedId
    private Folder_Documents_ID folder_documents_compositeKey  = new Folder_Documents_ID();

    @Column
    private Date date;

    @Column
    private String status;

Folder_Document(连接表)

@Embeddable
public class Folder_Documents_ID implements Serializable { 

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @ManyToOne(cascade = CascadeType.ALL)
    private Folder folder;
    @ManyToOne(cascade = CascadeType.ALL)
    private Document document;

Folder_documents_id(复合键)

Document

问题是我无法在Folder_Documents的{​​{1}}属性中映射@AssociationOverrides compositeKey,因为hibernate在Document中找不到复合键ID和matricule属性}。文件夹引用很好。

有栈跟踪:

Caused by: org.hibernate.AnnotationException: referencedColumnNames(matricule) of com.renault.entity.Folder_Documents_ID.folder_documents_compositeKey.document referencing com.renault.entity.Document not mapped to a single property

1 个答案:

答案 0 :(得分:0)

已解决,AssociationOverride注释的语法错误

正确的语法:

Enable Development Signing