2016-11-25 10 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: failed träge, um eine Sammlung von Rolle zu initialisieren

Mein Entity:

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

Controller-Methode: (RESTful Web Service-API)

Mögliche Vorgänge in der Methode, die eine Ausnahme verursacht!

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-Methode:

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

Antwort

0

Wenn Sie das Objekt in die Zeichenfolge serialisieren, sollten Sie eifrig Initialisierung in Ihrem zugehörigen Hibernate Entitäten haben.

in Ihrem Fall

@OneToMany(fetch = FetchType.LAZY, mappedBy = "courseMaster") 
public Set<ChapterCourseMapping> getChapterCourseMappings() { 
return this.chapterCourseMappings; 
} 

als unter

@OneToMany(fetch = FetchType.EAGER, mappedBy = "courseMaster") 
public Set<ChapterCourseMapping> getChapterCourseMappings() { 
return this.chapterCourseMappings; 
} 

Auch alle Verbände sollten holen eifrig überprüfen umbenennen.

ODER ELSE können Sie erstellen SQlQueries in Hibernate und zuordnen Sie dieses Ergebnis zu einigen pojo und konvertieren Sie das in JSON-Zeichenfolge. es wird funktionieren.

Verwandte Themen