我有一些桌子:
PROFIL:id_profil,...
体验:id_experience,id_profil#,...
COMPETENCE_LEVEL:id_competence_level,级别,...
一个PROFIL可以有很多经验和很多COMPETENCE_LEVEL。
一项体验可以拥有很多COMPETENCE_LEVEL。
一个COMPETENCE_LEVEL涉及很多经验。
因此,对我来说,这是EXPERIENCE和COMPETENCE_LEVEL之间的(n-p)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;
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;
}
@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"))
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;
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;
}
@ManyToMany(mappedBy = "competenceLevels")
public List<Experience> getExperiences() {
return experiences;
}
public void setExperiences(List<Experience> experiences) {
this.experiences = experiences;
}
@Override
public String toString() {
// [...]
}
}
所以,我有这个错误:
org.hibernate.MappingException: Could not determine type for: java.util.List, at table: competence_level, for columns: [org.hibernate.mapping.Column(experiences)]
我不明白为什么。我遵循此教程:https://hellokoding.com/jpa-many-to-many-relationship-mapping-example-with-spring-boot-maven-and-mysql/
你有想法吗 ?谢谢。
最佳答案
原因很简单:不要在同一持久性类中混合字段和方法注释。
Hibernate在这里生成了一个不清楚的错误。如果您以前没有遇到过错误,很难弄清错误的原因。