ulquiorra ulquiorra - 2 months ago 32
Java Question

jpa many to many with additionnal column and composite key

I have 2 tables :folder(simple primary key) and document(composite primary key)

I want a join table named folder_documents which will contains the id of both tables with additionnal columns

There is my entities :

Folder

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


Document

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

@EmbeddedId
private DocumentID documentCompositeKey;

@Column
private Date date;


DocumentID(composite key)

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


Folder_Document ( join table)

@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(composite key)

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


The problem is i can't map the
Document
compositeKey in
Folder_Documents
' s
@AssociationOverrides
attributes because hibernate don't find the composite key id and matricule properties in
Document
. Folder references is fine .

There is the stacktrace :

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


Any help would be appreciated

Thank you very much

Answer

Resolved , the syntax of the AssociationOverride annotation was wrong

Correct syntax :

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") })})
Comments