我有一些错误要解决,这是错误:
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:165)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:286)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185)
at model.Teacher_$$_jvstd69_0.toString(Teacher_$$_jvstd69_0.java)
at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getFormattedValue(HtmlBasicRenderer.java:517)
at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getFormattedValue(HtmlBasicRenderer.java:540)
at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:357)
at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeEnd(HtmlBasicRenderer.java:164)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1903)
at org.primefaces.component.column.Column.renderChildren(Column.java:344)
at org.primefaces.component.datatable.DataTableRenderer.encodeCell(DataTableRenderer.java:1019)
at org.primefaces.component.datatable.DataTableRenderer.encodeRow(DataTableRenderer.java:967)
at org.primefaces.component.datatable.DataTableRenderer.encodeRows(DataTableRenderer.java:878)
at org.primefaces.component.datatable.DataTableRenderer.encodeTbody(DataTableRenderer.java:825)
at org.primefaces.component.datatable.DataTableRenderer.encodeTbody(DataTableRenderer.java:788)
at org.primefaces.component.datatable.DataTableRenderer.encodeRegularTable(DataTableRenderer.java:281)
at org.primefaces.component.datatable.DataTableRenderer.encodeMarkup(DataTableRenderer.java:243)
at org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:85)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1903)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1899)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1899)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:451)
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1521)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1478)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
它发生在我从Hibernate工具
生成的关系2表之后表学生
@Entity
@Table(name = "student", schema = "public")
public class Student implements java.io.Serializable {
private static final long serialVersionUID = 3484556118289412550L;
private int stuId;
private Teacher teacher;
private String stuName;
private Integer stuAge;
public Student() {
}
public Student(int stuId) {
this.stuId = stuId;
}
public Student(int stuId, Teacher teacher, String stuName, Integer stuAge) {
this.stuId = stuId;
this.teacher = teacher;
this.stuName = stuName;
this.stuAge = stuAge;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "stu_id", unique = true, nullable = false)
public int getStuId() {
return this.stuId;
}
public void setStuId(int stuId) {
this.stuId = stuId;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "stu_teachid")
public Teacher getTeacher() {
return this.teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
@Column(name = "stu_name", length = 40)
public String getStuName() {
return this.stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
@Column(name = "stu_age")
public Integer getStuAge() {
return this.stuAge;
}
public void setStuAge(Integer stuAge) {
this.stuAge = stuAge;
}
}
老师
@Entity
@Table(name = "teacher", schema = "public")
public class Teacher implements java.io.Serializable {
private static final long serialVersionUID = 3863374948774698083L;
private int teachId;
private String teachName;
private Set<Student> students = new HashSet<Student>(0);
public Teacher() {
}
public Teacher(int teachId) {
this.teachId = teachId;
}
public Teacher(int teachId, String teachName, Set<Student> students) {
this.teachId = teachId;
this.teachName = teachName;
this.students = students;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "teach_id", unique = true, nullable = false)
public int getTeachId() {
return this.teachId;
}
public void setTeachId(int teachId) {
this.teachId = teachId;
}
@Column(name = "teach_name", length = 40)
public String getTeachName() {
return this.teachName;
}
public void setTeachName(String teachName) {
this.teachName = teachName;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "teacher")
public Set<Student> getStudents() {
return this.students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
}
我的applicationContext.xml
<context:component-scan base-package="controller"></context:component-scan>
<context:component-scan base-package="dao"></context:component-scan>
<context:component-scan base-package="model"></context:component-scan>
<context:component-scan base-package="service"></context:component-scan>
<!-- Enable Spring Annotation Configuration -->
<context:annotation-config />
<context:spring-configured />
<!-- Create Data Source bean -->
<bean id="DataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5432/university" />
<property name="username" value="postgres" />
<property name="password" value="1234" />
</bean>
<!-- Define SessionFactory bean -->
<bean id="SessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="DataSource" />
<property name="annotatedClasses">
<list>
<value>model.Student</value>
<value>model.Teacher</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.use_sql_comments">true</prop>
<prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
</props>
</property>
</bean>
<!-- Detect @Transactional Annotation -->
<tx:annotation-driven transaction-manager="txManager" />
<!-- Transaction Manager is defined -->
<bean id="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="SessionFactory" />
</bean>
如何解决这个问题?
答案 0 :(得分:1)
有三种方法可以避免延迟初始化异常:
在映射文件中将lazy属性设置为false。我不推荐这种方法,因为它会增加数据库负载,因此会降低性能。
保持会话开放。在处理数据之前,请勿关闭会话。如果会话在请求期间处于打开状态,您可以获取关联的图表,但您需要确保该操作在同一事务中进行。
急切地获取关联。在HQL查询中,使用关键字“fetch”来检索关联。从我的角度来看,这是避免延迟初始化问题的最佳解决方案。在HQL中,您只需要在from子句中添加fetch关键字以急切地获取关联。
答案 1 :(得分:0)
将学生组的映射从LAZY更改为EAGER