new post
Hi,
I have three entities Document, Author and DocumentAuthors.
Document contains set of DocumentAuthors and DocumentAuthors has just 2 foreign keys - DocumentId and AuthorId.
I am trying to achieve following:
To get list of all documents written by specified author(s), or other way around to get author(s) who wrote specified document(s).
No sure how I should map this so it would work both ways.
Getting query done would actually be step 2...right now I cannot get even index created properly.
Code:
/**
* Document entity.
*
*/
@Entity
@Indexed
@Table(name = "document", catalog = "library", uniqueConstraints = {})
public class Document implements java.io.Serializable {
// Fields
@Id
@Column(name = "documentId", unique = true, nullable = false, insertable = true, updatable = true)
@DocumentId
private Long documentId;
private String documentName;
....
@IndexedEmbedded
@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "document")
public Set<DocumentAuthors> getDocumentAuthors() {
return this.documentAuthors;
}
@Field(index=Index.TOKENIZED)
@Column(name = "DocumentName", unique = false, nullable = true, insertable = true, updatable = true, length = 256)
public String getDocumentName() {
return this.documentName;
}
Code:
/**
* DocumentAuthors entity.
*
*/
@Entity
@Indexed
@Table(name = "documentAuthors", catalog = "library", uniqueConstraints = {})
public class DocumentAuthors implements java.io.Serializable {
// Fields
private DocumentAuthorsId id;
private Document document;
private Author author;
// Property accessors
@EmbeddedId
@DocumentId
@AttributeOverrides( {
@AttributeOverride(name = "documentId", column = @Column(name = "DocumentID", unique = false, nullable = false, insertable = true, updatable = true)),
@AttributeOverride(name = "authorId", column = @Column(name = "AuthorID", unique = false, nullable = false, insertable = true, updatable = true)) })
public DocumentAuthorsId getId() {
return this.id;
}
@IndexedEmbedded
@ManyToOne(cascade = {}, fetch = FetchType.LAZY)
@JoinColumn(name = "documentId", unique = false, nullable = false, insertable = false, updatable = false)
public Document getDocument() {
return this.document;
}
@IndexedEmbedded
@ManyToOne(cascade = {}, fetch = FetchType.LAZY)
@JoinColumn(name = "Author_ID", unique = false, nullable = false, insertable = false, updatable = false)
public Author getAuthor() {
return this.author;
}
}
Code:
/**
* Author entity.
*
*/
@Entity
@Indexed
@Table(name = "author", catalog = "library", uniqueConstraints = {})
public class Author implements java.io.Serializable {
// Fields
private Long authorId;
private String authorName;
private Set<DocumentAuthors> documentAuthors = new HashSet<Objectauthor>(0);
private Set<DocumentAuthors> documentAuthors_1 = new HashSet<Objectauthor>(0);
// Property accessors
@Id
@DocumentId
@Column(name = "Author_ID", unique = true, nullable = false, insertable = true, updatable = true)
public Long getAuthorId() {
return this.authorId;
}
@Field(index=Index.TOKENIZED)
@Column(name = "Author_Name", unique = false, nullable = false, insertable = true, updatable = true, length = 128)
public String getAuthorName() {
return this.authorName;
}
@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "author")
public Set<DocumentAuthors> getDocumentAuthors() {
return this.documentAuthors;
}
@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "author")
public Set<DocumentAuthors> getDocumentAuthors_1() {
return this.documentAuthors_1;
}
}