Chaibi Alaa
Chaibi Alaa

Reputation: 1386

Could not determine a type by Hibernate

When I launch my Spring Hibernate App, I get this error :

Error creating bean with name 'manageUserDAO': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory edusef.dao.ManageUserDAO.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'SessionFactory' defined in ServletContext resource [/WEB-INF/application-context.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: Could not determine type for: edusef.model.Roles, at table: USER, for columns: [org.hibernate.mapping.Column(roles)]

I'm not a real guru on J2EE and this almost my first advanced app. I don't know what is happening there, here is my User and Roles models :

package edusef.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.CascadeType;
import javax.persistence.JoinColumn;
import javax.persistence.Table;
import javax.persistence.ManyToOne;
import java.util.Date;



@Entity
@Table (name="USER")
public class User {
    private int idUser;
    private String NomUser;
    private String PrenUser;
    private String MailUser;
    private String AdressUser;
    private int PhoneUser;
    private Date DateNaissanceUser;
    private int AccountStatus;
    private String Password;
    private String Login;
    private char SexeUser;
    private String ImagePath;
     private int idRole;


     @Id
     @GeneratedValue(strategy = GenerationType.AUTO)
     @Column(name="IDUSER", unique = true, nullable = false)
     public int getidUser() {     return idUser;     }
     public void setidUser(int iduser) {      this.idUser = iduser;  }

     @Column(name="NOMUSER", nullable = false)
     public String getNomUser() {     return NomUser;    }
     public void setNomUser(String nomUser) {     this.NomUser = nomUser;    }

     @Column(name="PRENUSER", nullable = false)
     public String getPrenUser() {    return PrenUser;   }
     public void setPrenUser(String prenUser) {   this.PrenUser = prenUser;  }

     @Column(name="MAILUSER", nullable = false)
     public String getMailUser() {    return MailUser;   }
     public void setMailUser(String mailUser) {   this.MailUser = mailUser;  }

     @Column(name="ADRESSUSER", nullable = false)
     public String getAdressUser() {      return AdressUser;     }
     public void setAdressUser(String adressUser) {   this.AdressUser = adressUser;  }

     @Column(name="PHONEUSER", nullable = false)
     public int getPhoneUser() {      return PhoneUser;      }
     public void setPhoneUser(int phoneUser) {    this.PhoneUser = phoneUser;    }

     @Column(name="DATENAISSANCEUSER", nullable = false)
     public Date getDateNaissanceUser() {     return DateNaissanceUser;      }
     public void setDateNaissanceUser(Date dateNaissanceUser) {   this.DateNaissanceUser = dateNaissanceUser;    }

     @Column(name="ACCOUNTSTATUS", nullable = false)
     public int getAccountStatus() {      return AccountStatus;      }
     public void setAccountStatus(int accountStatus) {    this.AccountStatus = accountStatus;    }

     @Column(name="PASSWORD", nullable = false)
     public String getPassword() {    return Password;   }
     public void setPassword(String password) {   this.Password = password;  }

     @Column(name="LOGIN", nullable = false)
     public String getLogin() {   return Login;      }
     public void setLogin(String login) {     this.Login = login;    }

     @Column(name="SEXEUSER", nullable = false)
     public char getSexeUser() {      return SexeUser;   }
     public void setSexeUser(char sexeUser) {     this.SexeUser = sexeUser;  }

     @Column(name="IMAGEPATH", nullable = true)
     public String getImagePath() {   return ImagePath;      }
     public void setImagePath(String imagePath) {     this.ImagePath = imagePath;    }

     @ManyToOne(cascade = CascadeType.ALL)
     @JoinColumn(name="IDROLE", nullable=false)
    public int getidRole() {      return idRole;     }
    public void setidRole(int idRole) {   this.idRole = idRole;  }
     private Roles roles;
    public Roles getRoles() {
        return roles;
    }
    public void setRoles(Roles roles) {
        this.roles = roles;
    }

     @Override
     public String toString() {
      StringBuffer strBuff = new StringBuffer();
      strBuff.append("idUser : ").append(getidUser());
      strBuff.append(", NomUser : ").append(getNomUser());
      strBuff.append(", PrenUser : ").append(getPrenUser());
      strBuff.append(", MailUser : ").append(getMailUser());
      strBuff.append(", AdressUser : ").append(getAdressUser());
      strBuff.append(", PhoneUser : ").append(getPhoneUser());
      strBuff.append(", DateNaissanceUser : ").append(getDateNaissanceUser());
      strBuff.append(", AccountStatus : ").append(getAccountStatus());
      strBuff.append(", Password : ").append(getPassword());
      strBuff.append(", Login : ").append(getLogin());
      strBuff.append(", SexeUser : ").append(getSexeUser());
      strBuff.append(", ImagePath : ").append(getImagePath());
      // strBuff.append(", idRole : ").append(getidRole());
      return strBuff.toString();
     }


}

Roles :

package edusef.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;

import javax.persistence.Table;

@Entity
@Table (name="ROLES")
public class Roles {

    private int idRole;
    private String Role;

    @Id
    @Column(name="IDROLE", unique = true, nullable = false)
    public int getIdRole() {    return idRole;  }
    public void setIdRole(int idRole) { this.idRole = idRole;   }

    @Column(name="ROLES", nullable = false)
    public String getRole() { return Role;  }
    public void setRole(String role) {  Role = role;    }
}

ManageUserDAO :

package edusef.dao;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import edusef.model.User;

import java.util.List;

@Repository
public class ManageUserDAO {

    public @interface ComponentScan {

    }
    @Autowired
    private SessionFactory sessionFactory;
    public SessionFactory getSessionFactory() { return sessionFactory; }
    public void setSessionFactory(SessionFactory sessionFactory) {this.sessionFactory = sessionFactory;}


    public void addUser(User user) {
        getSessionFactory().getCurrentSession().save(user);
    }

    public void deleteUser(User user) {
        getSessionFactory().getCurrentSession().delete(user);
    }

    public void updateUser(User user) {
        getSessionFactory().getCurrentSession().update(user);
    }

    public User getUserById(int id) {
        List list = getSessionFactory().getCurrentSession().createQuery("from User where id=?").setParameter(0, id).list();
        return (User)list.get(0);
    }

    public List<User> getUsers() {
        List list = getSessionFactory().getCurrentSession().createQuery("from User").list();
        return list;
    }


}

RolesDAOImpl :

public class RolesDAOImpl implements RolesDAO {
    @Autowired
    private SessionFactory sessionFactory;

    private Session getCurrentSession() {
        return sessionFactory.getCurrentSession();
    }

    public Roles getRoles(int idRole) {
        Roles role = (Roles) getCurrentSession().load(Roles.class, idRole);
        return role;
    }
}

My Logic is that each User has one Role (ManyToOne) and each role can be used by many users. the User.idRole entity should return the idRole of the User and depending on it we test the user role on Spring Security ..

Any help is welcome ! Thanks

Upvotes: 0

Views: 180

Answers (1)

Kayaman
Kayaman

Reputation: 73558

You're trying to map an int with @ManyToOne in your User class. You should be mapping a Role instead.

Remove the getIdRole() and setIdRole() methods from this part, they don't belong there.

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name="IDROLE", nullable=false)
public int getidRole() {      return idRole;     }
public void setidRole(int idRole) {   this.idRole = idRole;  }
private Roles roles;
public Roles getRoles() {
    return roles;
}

Upvotes: 1

Related Questions