简体   繁体   English

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

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

I have some tables : 我有一些桌子:

PROFIL : id_profil, ... PROFIL :id_profil,...

EXPERIENCE : id_experience, id_profil#, ... 体验 :id_experience,id_profil#,...

COMPETENCE_LEVEL : id_competence_level, level, ... COMPETENCE_LEVEL :id_competence_level,级别,...

One PROFIL can have lot of EXPERIENCE and lot of COMPETENCE_LEVEL . 一个PROFIL可以具有很多经验COMPETENCE_LEVEL

One EXPERIENCE can have lot of COMPETENCE_LEVEL . 一个经验可以有很多COMPETENCE_LEVEL

One COMPETENCE_LEVEL concerns lot of EXPERIENCE . 一个COMPETENCE_LEVEL涉及到很多经验

So, for me, between EXPERIENCE and COMPETENCE_LEVEL, this is a (np) ManyToMany relation. 因此,对我而言,这是EXPERIENCE和COMPETENCE_LEVEL之间的(np) ManyToMany关系。

I tried: 我试过了:

PROFIL.java: 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: 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: 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() {
        // [...]
    }

}

So, I have this error : 所以,我有这个错误:

org.hibernate.MappingException: Could not determine type for: java.util.List, at table: competence_level, for columns: [org.hibernate.mapping.Column(experiences)]

I don't understand why. 我不明白为什么。 I follow this tuto : https://hellokoding.com/jpa-many-to-many-relationship-mapping-example-with-spring-boot-maven-and-mysql/ 我遵循这个教程: https ://hellokoding.com/jpa-many-to-many-relationship-mapping-example-with-spring-boot-maven-and-mysql/

Do you have an idea ? 你有想法吗 ? Thanks. 谢谢。

The reason is simply: don't mix field and method annotations in the same persistent class. 原因很简单:不要在同一个持久化类中混合字段和方法注释。

Hibernate generates an unclear error here. Hibernate在这里生成了一个不清楚的错误。 It is very hard to figure out the reason of the error, if you don't face it before. 如果您以前没有遇到过错误,很难弄清错误的原因。

In your code, you are mixing field access and property access. 在您的代码中,您将字段访问和属性访问混合在一起。 See this answer . 看到这个答案

I would prefer using only one of the possibilities. 我希望仅使用一种可能性。 I use field annotations, like you did for idProfil . 我使用字段注释,就像对idProfil所做的idProfil

In the book "Professional Java for Web Applications" by Nicholas S. Williams (very, very good) I found this: 在尼古拉斯·威廉姆斯(Nicholas S. Williams)的“ Web应用程序专业Java”一书中(非常好),我发现了这一点:

You should never mix JPA property annotations and JPA field annotations in the same entity. 永远不要 在同一实体中 混合使用JPA属性注释和JPA字段 注释。 Doing so results in unspecified behaviour and is very likely to cause errors. 这样做会导致未指定的 行为,并且很可能导致错误。

And just for clearness, I wouldn't write this 为了清楚起见,我不会写这个

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法确定类型:java.util.List - Could not determine type for: java.util.List org.hibernate.MappingException:无法确定类型:java.util.List,在表:大学,列:[org.hibernate.mapping.Column(students)] - org.hibernate.MappingException: Could not determine type for: java.util.List, at table: College, for columns: [org.hibernate.mapping.Column(students)] org.hibernate.MappingException:无法确定类型:java.util.List,在表:Schedule_assignedRoles,对于列:AssignedRoles - org.hibernate.MappingException: Could not determine type for: java.util.List, at table: Schedule_assignedRoles, for columns: assignedRoles org.hibernate.MappingException:无法确定类型:java.util.List,在表:user处,用于列:[org.hibernate.mapping.Column(events)] - org.hibernate.MappingException: Could not determine type for: java.util.List, at table: user, for columns: [org.hibernate.mapping.Column(events)] Spring:无法确定类型:java.util.List - Spring: Could not determine type for: java.util.List hibernate MappingException:无法确定类型:java.util.Set - hibernate MappingException: Could not determine type for: java.util.Set 无法确定类型:java.util.List,在表:file_post,列:[org.hibernate.mapping.Column(file)] - Could not determine type for: java.util.List, at table: file_post, for columns: [org.hibernate.mapping.Column(file)] 更改java.util.List的输出类型 - change type of the output of java.util.List 泛型类型java.util.List继承 - Generic type java.util.List inheritance 获取 java.util.List 的泛型类型 - Get generic type of java.util.List
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM