问题
在Hibernate开发中,通过注释定义了一对多关系。
package com.mkyong.user.model;
@Entity
@Table(name = "USER", schema = "MKYONG")
public class User implements java.io.Serializable {
private Set address = new HashSet(0);
//...
@OneToMany(orphanRemoval=true,
cascade=CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "user")
public Set getAddress() {
return this.address;
}
}
但是它遇到以下异常:
Initial SessionFactory creation failed. org.hibernate.AnnotationException:
Collection has neither generic type or OneToMany.targetEntity() defined: com.mkyong.user.model.user
解
“用户”类具有原始类型集合“ 设置地址 ”,并且Hibernate不支持此设置,因为Hibernate不知道要与哪个“类”链接。
例如,
- 设置地址; // Set是原始类型,Hibernate返回异常。
- 设置<地址>地址; // Hibernate现在知道Set is Address类。
因此,您的班级需要像这样更改:
package com.mkyong.user.model;
@Entity
@Table(name = "USER", schema = "MKYONG")
public class User implements java.io.Serializable {
private Set<Address> address = new HashSet<Address>(0);
//...
@OneToMany(orphanRemoval=true,
cascade=CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "user")
public Set<Address> getAddress() {
return this.address;
}
}
标签: 冬眠