Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate @OneToMany and UNIQUE constraint

Tags:

hibernate

I'm using Hibernate to store information about article citations. And I annotated my class in this way in order to express a relationship between two articles.

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(name = "CITATIONS")
private Set<Article> citingArticles = new HashSet<Article>();

Unfortunately this is translated with a UNIQUE constraint on the citingArticle, which means that I can only have an article to cite a single other article.

Of course this is not what I'd like to have, how can I remove the UNIQUE constraint?

like image 940
mariosangiorgio Avatar asked Jan 10 '12 14:01

mariosangiorgio


Video Answer


1 Answers

If you have a many-to-many relationship, you need to model it with @ManyToMany:

@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) 
@JoinTable(name = "CITATIONS") 
private Set<Article> citingArticles = new HashSet<Article>(); 
like image 64
axtavt Avatar answered Dec 08 '22 09:12

axtavt