簡體   English   中英

jpa多對多,帶有附加列和復合鍵

[英]jpa many to many with additional column and composite key

我有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和@AssociationOverrides屬性。 文件夾引用很好。

有堆棧跟蹤:

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

已解決,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") })})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM