org.hibernate.MappingException 出错

Posted

技术标签:

【中文标题】org.hibernate.MappingException 出错【英文标题】:Error with org.hibernate.MappingException 【发布时间】:2017-03-02 10:44:17 【问题描述】:

当我想运行我的 spring mvc 时遇到问题。这是按名称和角色将用户插入数据库的模型

所以,从数据库中获取实体后,我有了这些文件 App_User

@Entity
@Table(name = "app_user", schema = "spring")
public class AppUser 
    private int id;
    private String nome;
    private String email;
    private String password;
    private String telefone;
    private String tipo;
    private byte estado;
    private byte coordenador;
    private Set<UserProfile> userProfiles;

    @Id
    @Column(name = "id")
    public int getId() 
        return id;
    

    public void setId(int id) 
        this.id = id;
    

    @Basic
    @Column(name = "nome")
    public String getNome() 
        return nome;
    

    public void setNome(String nome) 
        this.nome = nome;
    

    @Basic
    @Column(name = "email")
    public String getEmail() 
        return email;
    

    public void setEmail(String email) 
        this.email = email;
    

    @Basic
    @Column(name = "password")
    public String getPassword() 
        return password;
    

    public void setPassword(String password) 
        this.password = password;
    

    @Basic
    @Column(name = "telefone")
    public String getTelefone() 
        return telefone;
    

    public void setTelefone(String telefone) 
        this.telefone = telefone;
    

    @Basic
    @Column(name = "tipo")
    public String getTipo() 
        return tipo;
    

    public void setTipo(String tipo) 
        this.tipo = tipo;
    

    @Basic
    @Column(name = "estado")
    public byte getEstado() 
        return estado;
    

    public void setEstado(byte estado) 
        this.estado = estado;
    

    @Basic
    @Column(name = "coordenador")
    public byte getCoordenador() 
        return coordenador;
    

    public void setCoordenador(byte coordenador) 
        this.coordenador = coordenador;
    

    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "APP_USER_USER_PROFILE", joinColumns = 
            @JoinColumn(name = "USER_ID", nullable = false, updatable = false) ,
            inverseJoinColumns =  @JoinColumn(name = "USER_PROFILE_ID",
                    nullable = false, updatable = false) )
    public Set<UserProfile> getUserProfiles() 
        return userProfiles;
    

    public void setUserProfiles(Set<UserProfile> userProfiles) 
        this.userProfiles = userProfiles;
    



用户个人资料

@Entity
@Table(name="user_profile")
public class UserProfile implements Serializable 

    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;

    @Column(name="TYPE", length=15, unique=true, nullable=false)
    private String type = UserProfileType.USER.getUserProfileType();

    public Integer getId() 
        return id;
    

    public void setId(Integer id) 
        this.id = id;
    

    public String getType() 
        return type;
    

    public void setType(String type) 
        this.type = type;
    

    private Set<AppUser> userProfiles;

    @ManyToMany(fetch = FetchType.LAZY, mappedBy = "userProfiles")
    public Set<AppUser> getUserProfiles() 
        return userProfiles;
    

    public void setUserProfiles(Set<AppUser> userProfiles) 
        this.userProfiles = userProfiles;
    

    @Override
    public String toString() 
        return "UserProfile [id=" + id + ", type=" + type + "]";
    


当我运行 tomcat 服务器时出现此错误

Caused by: org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: user_profile, for columns: [org.hibernate.mapping.Column(userProfiles)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:349)
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:322)
at org.hibernate.mapping.Property.isValid(Property.java:241)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:496)
at org.hibernate.mapping.RootClass.validate(RootClass.java:270)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1360)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1851)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1930)

有什么帮助吗?

【问题讨论】:

【参考方案1】:

您已在方法级别添加注释:

@ManyToMany(fetch = FetchType.LAZY, mappedBy = "userProfiles")
    public Set<AppUser> getUserProfiles() 
        return userProfiles;
    

将注释移至您的字段:

@ManyToMany(fetch = FetchType.LAZY, mappedBy = "userProfiles")
private Set<AppUser> userProfiles;

您必须选择字段级别的注释或方法级别。

更多详情请参考这篇文章:Hibernate/JPA - annotating bean methods vs fields

如果你注释了字段,Hibernate 将使用字段访问来设置 并获得这些领域。如果您注释方法,休眠将使用 吸气剂和二传手。 Hibernate 将根据 @Id 注释的位置,据我所知,您不能混用 和匹配。如果您使用@Id 注释字段,则方法上的注释 将被忽略,反之亦然。

【讨论】:

以上是关于org.hibernate.MappingException 出错的主要内容,如果未能解决你的问题,请参考以下文章