i would like to create a many to many relationship between two tables "Term" and "Synonym" but i dont now how to write them Models !! Any Help ! and thnx a lot.
Asked
Active
Viewed 116 times
-1
-
Welcome to stackoverflow. Please include a clear problem description and if possible some minimal code. For guidance please check the [how to ask](https://stackoverflow.com/help/how-to-ask) page and [how to create a minimal example](https://stackoverflow.com/help/mcve). – 5th Jul 29 '18 at 20:33
-
ok i will thnx ! – Samer Sboui Jul 29 '18 at 20:44
1 Answers
0
For that you will need 2 models. In Term
class:
@Entity
public class Term extends Models {
@Id
private String id;
@ManyToMany
private List<Synonym> synonyms = new ArrayList<>();
}
In Synonym
class:
@Entity
public class Synonym extends Models {
@Id
private String id;
@ManyToMany
@JoinTable
private List<Term> terms = new ArrayList<>();
}
And for your database structure it will look like this:
CREATE TABLE term {
id CHAR(255)
}
CREATE TABLE synonym {
id CHAR(255)
}
CREATE TABLE term_synonym {
term_id CHAR(255),
synonym_id CHAR(255),
PRIMARY KEY(term_id, synonym) // This is needed to avoid having the same many to many relation
}
term_synonym
table is needed to maintain the relationship of @ManyToMany in database level.

hong823
- 21
- 4