我有一些表格:
PROFIL :id_profil,...
体验:id_experience,id_profil#,...
COMPETENCE_LEVEL :id_competence_level,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)]
我不明白为什么。我按照这个tuto:https://hellokoding.com/jpa-many-to-many-relationship-mapping-example-with-spring-boot-maven-and-mysql/
你知道吗?感谢。答案 0 :(得分:5)
原因很简单:不要在同一个持久化类中混合使用字段和方法注释。
Hibernate在这里生成一个不明确的错误。如果您以前没有面对错误,很难弄清楚错误的原因。
答案 1 :(得分:3)
在您的代码中,您正在混合字段访问和属性访问。请参阅this answer。
我更愿意只使用其中一种可能性。我使用字段注释,就像你对idProfil
所做的那样。
在书&#34;专业Java for Web Applications&#34;作者:Nicholas S. Williams(非常,非常好)我发现了这个:
您永远不应混用JPA属性注释和JPA字段 同一实体中的注释。这样做会导致未指定 行为并且极有可能导致错误。
只是为了清楚,我不会写这个
@ManyToOne
@JoinColumn(name="id_profil")
private Profil idProfil;
// better:
// private Profil profil;