jpa 多对多,带有附加列和复合键

Posted

技术标签:

【中文标题】jpa 多对多,带有附加列和复合键【英文标题】:jpa many to many with additional column and composite key 【发布时间】:2016-09-08 17:04:08 【问题描述】:

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

有我的实体:

文件夹

@Entity
@Table(name = "folder")

public class Folder 

    @Id
    @SequenceGenerator(name = "folder_seq_gen", sequenceName = "FOLDER_SEQ", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "folder_seq_gen")
    private long id;

    @Column
    private Date date;

    @OneToMany(mappedBy = "folder_documents_compositeKey.folder",
            cascade = CascadeType.ALL)
    private Set<Folder_Documents> folder_documents;

文档

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

    @EmbeddedId 
    private DocumentID documentCompositeKey;

    @Column
    private Date date;

DocumentID(复合键)

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

Folder_Document(连接表)

@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_documents_id(复合键)

@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@AssociationOverrides 属性中映射 Document 复合键,因为休眠在 Document 中找不到复合键 id 和矩阵属性。文件夹引用很好。

有堆栈跟踪:

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】:

已解决,AssociationOverride 注解语法错误

正确的语法:

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") ,
            @JoinColumn(name = "matricule" , referencedColumnName = "matricule") ))

【讨论】:

以上是关于jpa 多对多,带有附加列和复合键的主要内容,如果未能解决你的问题,请参考以下文章

带有额外列的 Spring Data JPA 多对多

带有额外列的 JPA 2.0 多对多 - 更新集合

多维数据集设计 - 带有附加列的多对多映射的桥接表

JPA 2:通过不在带有额外字段的多对多中工作来订购

Android Room - 带有附加字段的多对多关系

openjpa:多对多,带有额外的列