when use @MappedSuperclass,@Entity,@manytomany Could not determine type for: java.util.Set

1.3k views Asked by At
@MappedSuperclass
public abstract class IdEntity /*extends AbstractPersistable<Integer>*/
        implements Serializable,Cloneable {

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

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.setId(id);
    }
}


    @Entity
@Table(name = "user")
//@AttributeOverride( name="id", column = @Column(name="id") )
public class User extends IdEntity{

    @ManyToMany
    @JoinTable(name = "user_role",
            joinColumns = @JoinColumn(name = "user_id"),
            inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"))
    public Set<Role> getRoles() {
        return roles;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }

}


@Entity
@Table(name = "role")
//@AttributeOverride( name="id", column = @Column(name="id") )
public class Role extends IdEntity {

    private Set<User> users = new HashSet<User>();

    @ManyToMany(mappedBy = "roles")
    public Set<User> getUsers() {
        return users;
    }

    public void setUsers(Set<User> users) {
        this.users = users;
    }

}

Stack trace:

Caused by: javax.persistence.PersistenceException: [PersistenceUnit: com.rootls] Unable to build EntityManagerFactory
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:915)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:890)
    at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:74)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:286)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1545)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1483)
    ... 50 more
Caused by: org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: user, for columns: [org.hibernate.mapping.Column(roles)]
    at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:305)
    at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:289)
    at org.hibernate.mapping.Property.isValid(Property.java:238)
    at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:468)
    at org.hibernate.mapping.RootClass.validate(RootClass.java:270)
    at org.hibernate.cfg.Configuration.validate(Configuration.java:1294)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1736)
    at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:94)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:905)
    ... 56 more
2

There are 2 answers

1
V G On BEST ANSWER

The problem is that in your IdEntity class you annotated your ID field (not getter), and in your sub classes User and Role you annotated your getters, which is not consistent. You must annotate either ONLY the fields, or ONLY the getters. See this question for a similar problem.

Alternatively, add @Access(AccessType.PROPERTY) to your subclasses Role and User.

0
Lukas Eichler On

You have no attribute named Set<Role> roles in your User class. How were you able to compile this?