ManyToMany:MappingException:无法确定类型:java.util.List

ManyToMany : MappingException: Could not determine type for: java.util.List

我有一些桌子:

简介 : id_profil, ...

经验 : id_experience, id_profil#, ...

COMPETENCE_LEVEL : id_competence_level, 级别, ...

一个个人资料可以有很多经验和很多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/

你有想法吗?谢谢。

原因很简单:不要在同一个持久化中混合字段和方法注释class。

Hibernate 在这里产生了一个不清楚的错误。如果不面对错误,很难找出错误的原因。

在您的代码中,您混合了字段访问和 属性 访问。参见 this answer

我宁愿只使用其中一种可能性。我使用字段注释,就像您对 idProfil.

所做的那样

在 Nicholas S. Williams 的书 "Professional Java for Web Applications" 中(非常非常好)我发现了这个:

You should never mix JPA property annotations and JPA field annotations in the same entity. Doing so results in unspecified behaviour and is very likely to cause errors.

为了清楚起见,我不会写这个

@ManyToOne
@JoinColumn(name="id_profil")
private Profil idProfil;
// better:
// private Profil profil;