I'm having a problem to map a relationship between instances of one single entity. Let me give you the JPA entities first.
Article entity
@Entity
@Table(name = "article")
public class Article {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "article", orphanRemoval = true)
private Collection<Keyword> keywords;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "article1", orphanRemoval = true)
private Collection<RelatedArticles> relatedArticles;
@Column(name = "content", nullable = false)
@Lob
private String content;
...
}
RelatedArticle entity
@Entity
@Table(name = "related_articles")
public class RelatedArticles {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@JoinColumn(name = "article1_id")
@ManyToOne(optional = false)
private Article article1;
@JoinColumn(name = "article2_id")
@ManyToOne(optional = false)
private Article article2;
private Float weightedJaccardIndex;
...
}
Further explanation
An article can be related to other articles which is realized by the RelatedArticle entity. The article can be referenced by article1 or article2. That means the collection relatedArticles in Article should contain all instances of RelatedArticle where the ID either matches article1 or article2.
Question
How can I map a single collection of RelatedArticles in my Article entity where the origin Article is either article1 or article2?
Alternative solutions are welcome!