2 luowei505050 luowei505050 于 2013.12.05 11:15 提问

当使用 @MappedSuperclass,@Entity,@ManyToMany 报:Could not determine type for

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.(EntityManagerFactoryImpl.java:94)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:905)
... 56 more

@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;
    }

项目地址:https://github.com/luowei/mvn-test
sql脚本:

 create table user(
      id int(11) primary key auto_increment,
      username varchar(100) not null,
      password varchar(92) not null,
      email varchar(50)
    );

    create table role(
      id int(11) primary key auto_increment,
      role_name varchar(30) not null,
      role_description varchar(100),
      create_time timestamp not null

    );

    create table user_role(
      id int(11) primary key auto_increment,
      user_id int(11) not null,
      role_id int(11) not null,

      constraint uq_user_role unique(user_id, role_id)
    );

1个回答

luowei505050
luowei505050   2013.12.08 23:30
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
protected Integer id;

把IdEntity:
@Id
@GeneratedValue(strategy = GenerationType.AUTO)

移到get方法上去。。

imonHu
imonHu 回复我就看看w不乱说的: 正解!+1
9 个月之前 回复
qq_24708453
qq_24708453 我只是游客浏览,看到你的答案,我现注册了个号,只为说一句“正解,真特么赞答得”
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!