2016-11-25 104 views
0

net.sf.json.JSONException: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: dao.entities.CourseMaster.chapterCourseMappings, no session or session was closednet.sf.json.JSONException:org.hibernate.LazyInitializationException:无法初始化懒洋洋角色的集合

我的实体:

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

控制器方法:(REST的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

如果您序列化对象到字符串,你应该有渴望初始化您的相关休眠实体。

你的情况

@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字符串。它会起作用。

相关问题