There are tables: profession , subject . Tables are connected by a many-to-many relationship (the subject_has_profession table).

Domain, repository, service, Impliment were created for the profession , subject tables.

Do I need to create a separate subject_has_profession table? and for her (domain, repository, service, Impliment).

Subject

 @Entity @Table(name="subject") public class Subject { @Id @GeneratedValue(strategy= GenerationType.IDENTITY) @Column(name = "id") private Integer id; @Column(name = "SubjectName") private String subjectName; public Subject(String subjectName) { this.subjectName=subjectName; } public Subject( ){} public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getSubjectName() { return subjectName; } public void setSubjectName(String subjectName) { this.subjectName = subjectName; } /* Много(Subject)- ко Многим(Subjects) ???*/ @ManyToMany(fetch = FetchType.EAGER,cascade = {CascadeType.MERGE,CascadeType.PERSIST}) @JoinTable(name = "subject_has_subject", joinColumns =@JoinColumn(name = "Subject_id",referencedColumnName = "id",nullable = false), inverseJoinColumns = @JoinColumn(name = "Subject_id1",referencedColumnName = "id")) private Set<Subject1> subjects1; public Set<Subject1> getSubjects(){ return subjects1; } public void setSubjects(Set<Subject1> subjects1){ this.subjects1=subjects1; } /*Много(Subject)- ко Многим(Profession) */ @ManyToMany(fetch = FetchType.EAGER,cascade = {CascadeType.MERGE,CascadeType.PERSIST}) @JoinTable(name = "subject_has_profession", joinColumns ={@JoinColumn(name = "Subject_id",referencedColumnName = "id")}, inverseJoinColumns = {@JoinColumn(name = "Profession_id",referencedColumnName = "id")}) private Set<Profession> professions; public Set<Profession> getProfessions() { return professions; } public void setProfessions(Set<Profession> professions) { this.professions = professions; } } 

Profession

 @Entity @Table(name="profession") public class Profession { @Id @GeneratedValue(strategy= GenerationType.IDENTITY) @Column(name = "id") private Integer id; @Column(name = "Profession_Name") private String professionName; public Profession(){} public Profession(String professionName){ this.professionName =professionName; } public Integer getId(){ return id; } public void setId(Integer id) { this.id = id; } public String getProfessionName() { return professionName; } public void setProfessionName(String professionName) { this.professionName = professionName; } /*Много(Subject)- ко Многим(Profession) */ @ManyToMany(fetch = FetchType.EAGER,cascade = {CascadeType.MERGE,CascadeType.PERSIST},mappedBy = "professions") private Set<Subject> subjects; public Set<Subject> getSubjects() { return subjects; } public void setSubjects(Set<Subject> subjects) { this.subjects = subjects; } } 

Did according to this example http://devcolibri.com/2046

  • 2
    In JPA the link tables are not used anywhere except for the declaration of any Many . There are neither requests for them, nor any possibility to change them in the standard way. You don’t need to do anything with them. - Sergey

1 answer 1

Subject and Profession are entities. subject_has_profession is a link table and exists only in the database. Only Hibernate works directly with it, making a selection or modifying several tables, if necessary.

So far there are only two fields in it - two foreign keys - as its essence is not, it is not an entity, it is a connection. Now, if additional fields appear in it - then yes, it becomes an entity, ManyToMany converted into two pairs of @OneToMany and @ManyToOne and so on.