2017-03-02 35 views
0

我遇到了一個問題,當我想運行我的彈簧mvc。這是模型插入的名稱和角色的用戶到數據庫org.hibernate.MappingException的錯誤

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) 

任何幫助?

回答

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標註字段,則方法 上的註釋將被忽略,反之亦然。

相關問題