org.hibernate.MappingException出错

时间:2017-03-02 10:44:18

标签: hibernate spring-mvc

当我想要运行我的春季mvc时,我遇到了问题。这是通过名称和角色将用户插入数据库的模型

My database image

因此,从数据库获取实体后,我有这些文件 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;
    }


}

User_profile

@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 个答案:

答案 0 :(得分:2)

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

@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将使用   吸气剂和二传手。 Hibernate会选择基于的访问方法   @Id注释的位置,据我所知,你不能混用   并匹配。如果使用@Id注释字段,请在方法上注释   将被忽略,反之亦然。