@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;
}
}
堆栈追踪:
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
答案 0 :(得分:0)
问题在于,在您的IdEntity
课程中,您注释了您的ID字段(而非getter),并且在子类User
和Role
中注释了您的getter,这是不一致的。您必须仅注释字段,或仅注释getter。有关类似问题,请参阅this问题。
或者,将@Access(AccessType.PROPERTY)
添加到您的子类Role
和User
。
答案 1 :(得分:0)
您的Set<Role> roles
课程中没有名为User
的属性。你怎么能编译这个?