我在EXPERIENCE和COMPETENCE_LEVEL之间有一个ManyToMany关系。
我试过了:
PROFIL.java:
@Entity
@Table(name="profil")
public class Profil {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id_profil")
private Long idProfil;
public Profil() {
super();
}
public Long getIdProfil() {
return idProfil;
}
public void setIdProfil(Long idProfil) {
this.idProfil = idProfil;
}
@Override
public String toString() {
//[...]
}
}
EXPERIENCE.java:
@Entity
@Table(name="experience")
public class Experience {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id_experience")
private Long idExperience;
@ManyToOne
@JoinColumn(name="id_profil")
private Profil idProfil;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "experience_competence_level", joinColumns = @JoinColumn(name = "id_experience", referencedColumnName = "id_experience"), inverseJoinColumns = @JoinColumn(name = "id_competence_level", referencedColumnName = "id_competence_level"))
private List<CompetenceLevel> competenceLevels;
public Experience() {
super();
idProfil = new Profil();
}
public Long getIdExperience() {
return idExperience;
}
public void setIdExperience(Long idExperience) {
this.idExperience = idExperience;
}
public Profil getIdProfil() {
return idProfil;
}
public void setIdProfil(Profil idProfil) {
this.idProfil = idProfil;
}
public List<CompetenceLevel> getCompetenceLevels() {
return competenceLevels;
}
public void setCompetenceLevels(List<CompetenceLevel> competenceLevels) {
this.competenceLevels = competenceLevels;
}
@Override
public String toString() {
// [...]
}
}
COMPETENCE_LEVEL.java:
@Entity
@Table(name="competence_level")
public class CompetenceLevel {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id_competence_level")
private Long idCompetenceLevel;
@OneToOne
@JoinColumn(name="id_level")
private Level level;
@OneToOne
@JoinColumn(name="id_profil")
private Profil profil;
@ManyToMany(mappedBy = "competenceLevels")
private List<Experience> experiences;
public CompetenceLevel() {
super();
}
public Long getIdCompetenceLevel() {
return idCompetenceLevel;
}
public void setIdCompetenceLevel(Long idCompetenceLevel) {
this.idCompetenceLevel = idCompetenceLevel;
}
public Level getLevel() {
return level;
}
public void setLevel(Level level) {
this.level = level;
}
public Profil getProfil() {
return profil;
}
public void setProfil(Profil profil) {
this.profil = profil;
}
public List<Experience> getExperiences() {
return experiences;
}
public void setExperiences(List<Experience> experiences) {
this.experiences = experiences;
}
@Override
public String toString() {
// [...]
}
}
当我启动一个试图获得我所有经验的网络服务时,我的执行速度很快,感觉就像是一个stackoverflow问题(我不是专家):/。 但是,如果我删除了CompetenceLevel类中的List,它就可以了。
我不明白为什么。我按照这个tuto:https://hellokoding.com/jpa-many-to-many-relationship-mapping-example-with-spring-boot-maven-and-mysql/
你知道吗?感谢。