user5350210
user5350210

Reputation:

Error with org.hibernate.MappingException

I'm having a problem when i want to run my spring mvc. This is model to insert users by names and roles into the database

My database image

So, after getting the entity from database i have those files 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;
    }


}

The 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 + "]";
    }

}

When i run the tomcat server i getting this error

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)

Any help?

Upvotes: 0

Views: 155

Answers (1)

Learner
Learner

Reputation: 21393

You have added annotation on method level:

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

Move the annotation to your field:

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

Either you have to select field level annotations or method level.

Refer to this post for more details: Hibernate/JPA - annotating bean methods vs fields

if you annotate the fields, Hibernate will use field access to set and get those fields. If you annotate the methods, hibernate will use getters and setters. Hibernate will pick the access method based on the location of the @Id annotation and to my knowledge you cannot mix and match. If you annotate a field with @Id, annotations on methods will be ignored and visa versa.

Upvotes: 0

Related Questions