2016-11-25 18 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; 
} 
} 

コントローラ方法:(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

あなたは文字列にオブジェクトをシリアル化している場合、あなたは関連する休止状態のエンティティに熱心な初期化が必要です。あなたのケースで

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

以下のように名前を変更する。また、すべての関連付けを確認するには、フェッチ熱心でなければなりません。

OR ELSEでは、Hibernateでcreate SQlQueriesを使用して、その結果をいくつかのpojoにマップし、json文字列に変換できます。それは動作します。

関連する問題