0

我有以下类定义了StudentsCourses之间的 ManyToMany 关系。

课程类:

import java.io.Serializable;

import javax.persistence.*;

import java.util.HashSet;
import java.util.Set;

@Entity
@Table(name = "course")
public class Course implements Serializable {

private String id;
private String title;
private String sched;
private int    units;

// The students enrolled in this class
private Set<Student> students = new HashSet<Student>();

public Course() {
    // empty const required by java bean
}

public Course(String course_id, String title, String sched, int units) {
    this.id = course_id;
    this.title = title;
    this.sched = sched;
    this.units = units;
}


@Id
@Column(name = "COURSE_ID")
public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

    // Other setters/getters with mapping...

@ManyToMany
    @JoinTable(name = "student_course_join",
         joinColumns = @JoinColumn(name = "COURSE_ID"),
         inverseJoinColumns = @JoinColumn(name = "STUDENT_ID"))
public Set<Student> getStudents() {
    return this.students;
   }

    public void setStudents(Set<Student> students) {
        this.students = students;
   }


   public String toString() {
    return "Course - Id: " + id + " Title: " + title + " Sched: " + 
            sched + "Units: " + units;
   }
}

学生班:

import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.*;
import org.hibernate.annotations.Cascade;

import java.io.Serializable;

import java.util.HashSet;
import java.util.Set;

@Entity
@Table(name = "student")
public class Student implements Serializable {

private Long id;
private String firstName;
private String lastName;

private Set<Course> courses = new HashSet<Course>();

public Student() {
    // empty constructor
}

public Student(String firstName, String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
}

public Student(String firstName, String lastName, Set<Course> course) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.courses = courses;
}

// Setters and getters with Hibernate annotations

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "ID")
public Long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

    // Other setters/getters....

@ManyToMany
@JoinTable(name = "student_course_join",
    joinColumns = @JoinColumn(name = "STUDENT_ID"),
    inverseJoinColumns = @JoinColumn(name = "COURSE_ID"))
public Set<Course> getCourses() {
    return this.courses;
}

public void setCourses(Set<Course> courses) {
    this.courses = courses;
}

public void addCourse(Course course) {  

    // get course list and add the course to it
    getCourses().add(course);
    course.addStudent(this);   

}

public void dropCourse(Course course) {

    // Remove the course from the course list
    getCourses().remove(course);
    course.dropStudent(this);   

}

} // Class

以下 HQL 查询工作正常:

Query query = session.createQuery("select distinct c from Course c left join fetch 
                                                         c.students where c.id = :id");
query.setParameter("id", id);

Course course = (Course)query.uniqueResult();

课程对象返回一个有效的学生列表。我无法仅查询课程信息。我正在使用以下查询:

Query query = session.createQuery("select distinct c from Course c where c.id = :id");
query.setParameter("id", id);
course = (Course)query.uniqueResult();

我收到以下 HQL 异常:

org.hibernate.LazyInitializationException:未能延迟初始化角色集合:
edu.univ.mht.domain.Course.students,没有会话或会话已关闭.....

在这种情况下,我对检索学生列表不感兴趣,但 Hibernate 似乎在抱怨与学生列表相关的事情。我正在搜索的课程有一个有效的学生名单。即使我在数据库中搜索没有学生列表的课程,我也会收到此异常。

这是为查询生成的 HQL:

Hibernate: select course0_.COURSE_ID as COURSE1_0_, course0_.SCHED as SCHED0_, course0_.TITLE as TITLE0_, course0_.UNITS as UNITS0_ from course course0_ where course0_.COURSE_ID=?

这是堆栈跟踪:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: edu.univ.mht.domain.Course.students, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375)
at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:122)
at org.hibernate.collection.PersistentSet.isEmpty(PersistentSet.java:169)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at javax.el.BeanELResolver.getValue(BeanELResolver.java:87)
at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:67)
at org.apache.el.parser.AstValue.getValue(AstValue.java:183)
at org.apache.el.parser.AstNot.getValue(AstNot.java:44)
at org.apache.el.parser.AstAnd.getValue(AstAnd.java:43)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185)
at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:986)
at org.apache.jsp.WEB_002dINF.views.course_jsp._jspx_meth_c_005fif_005f1(course_jsp.java:167)
at org.apache.jsp.WEB_002dINF.views.course_jsp._jspService(course_jsp.java:93)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:487)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:412)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339)
at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1180)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
May 8, 2013 7:28:28 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [appServlet] in context with path [/mht] threw 
exception [javax.el.ELException: Error reading 'empty' on type   
org.hibernate.collection.PersistentSet] with root cause
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of 
role: edu.univ.mht.domain.Course.students, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375)
at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:122)
at org.hibernate.collection.PersistentSet.isEmpty(PersistentSet.java:169)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at javax.el.BeanELResolver.getValue(BeanELResolver.java:87)
at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:67)
at org.apache.el.parser.AstValue.getValue(AstValue.java:183)
at org.apache.el.parser.AstNot.getValue(AstNot.java:44)
at org.apache.el.parser.AstAnd.getValue(AstAnd.java:43)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185)
at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:986)
at org.apache.jsp.WEB_002dINF.views.course_jsp._jspx_meth_c_005fif_005f1(course_jsp.java:167)
at org.apache.jsp.WEB_002dINF.views.course_jsp._jspService(course_jsp.java:93)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:487)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:412)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339)
at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1180)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
4

0 回答 0