net.sf.json.JSONException: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: dao.entities.CourseMaster.chapterCourseMappings, no session or session was closed
我的实体:
@Entity
@Table(name = "course_master")
public class CourseMaster implements Serializable {
private Integer id;
private String courseName;
private String category;
private Set<CourseWeightage> courseWeightages = new HashSet<CourseWeightage>();
private Set<ChapterCourseMapping> chapterCourseMappings = new HashSet<ChapterCourseMapping>();
public CourseMaster() {
}
public CourseMaster(String courseName, String category) {
this.courseName = courseName;
this.category = category;
}
public CourseMaster(String courseName, String category,
Set<CourseWeightage> courseWeightages, Set<ChapterCourseMapping> chapterCourseMappings) {
this.courseName = courseName;
this.category = category;
this.courseWeightages = courseWeightages;
this.chapterCourseMappings = chapterCourseMappings;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "course_name", nullable = false, length = 100)
public String getCourseName() {
return this.courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
@Column(name = "category", nullable = false, length = 2)
public String getCategory() {
return this.category;
}
public void setCategory(String category) {
this.category = category;
}
@OneToMany(fetch = FetchType.EAGER, mappedBy = "courseMaster")
public Set<CourseWeightage> getCourseWeightages() {
return this.courseWeightages;
}
public void setCourseWeightages(Set<CourseWeightage> courseWeightages) {
this.courseWeightages = courseWeightages;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "courseMaster")
public Set<ChapterCourseMapping> getChapterCourseMappings() {
return this.chapterCourseMappings;
}
public void setChapterCourseMappings(Set<ChapterCourseMapping> chapterCourseMappings) {
this.chapterCourseMappings = chapterCourseMappings;
}
}
控制器方法:(RESTful Web服务API)
导致异常的方法中的潜在操作!
net.sf.json.JSONObject data = new net.sf.json.JSONObject();
List<CourseMaster> alrCourseMasters = genericBusiness.getCourses();
data.put("courselist", alrCourseMasters);
view = new Viewable("/test.jsp", data);
return Response.status(200).entity(view).build();
DAO方法:
public List<CourseMaster> getCourses()
{
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Query query = session.createQuery("from CourseMaster");
List<CourseMaster> alrCourseMasters = query.list();
transaction.commit();
session.flush();
session.close();
return alrCourseMasters;
}
答案 0 :(得分:0)
如果要将Object序列化为字符串,则应该在关联的hibernate实体中进行初始化。
在你的情况下,
@OneToMany(fetch = FetchType.LAZY, mappedBy = "courseMaster")
public Set<ChapterCourseMapping> getChapterCourseMappings() {
return this.chapterCourseMappings;
}
重命名为
@OneToMany(fetch = FetchType.EAGER, mappedBy = "courseMaster")
public Set<ChapterCourseMapping> getChapterCourseMappings() {
return this.chapterCourseMappings;
}
同时检查所有关联应该是急切的提取。
或者你可以在hibernate中使用create SQlQueries并将结果映射到某个pojo并将其转换为json字符串。它会起作用。