带有附加列和复合键的多对多 jpa
jpa many to many with additional column and composite key
我有 2 tables :folder(简单主键)和 document(复合主键)
我想要一个名为 folder_documents 的连接 table,它将包含两个 table 的 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 (加入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(复合键)
@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
复合键,因为休眠在 [= 中找不到复合键 ID 和矩阵属性16=]。文件夹引用没问题。
有堆栈跟踪:
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") })})
我有 2 tables :folder(简单主键)和 document(复合主键)
我想要一个名为 folder_documents 的连接 table,它将包含两个 table 的 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 (加入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(复合键)
@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
复合键,因为休眠在 [= 中找不到复合键 ID 和矩阵属性16=]。文件夹引用没问题。
有堆栈跟踪:
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") })})