org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:132)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:174)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
at com.zl.tzdb.entity.evaluate.KhRisktype_$$_javassist_9.getFxflName(KhRisktype_$$_javassist_9.java)
at org.usc.tests.KhRiskevualtionTest.getAll(KhRiskevualtionTest.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:44)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

解决方案 »

  1.   

    你还是贴出错的代码出来吧,错误原因是没有Session,所以需要知道你Session是在哪里初始化的。
      

  2.   


    package com.zl.tzdb.entity.evaluate;import java.sql.Timestamp;
    import java.util.Date;import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.FetchType;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.ManyToOne;
    import javax.persistence.Table;import com.zl.tzdb.entity.cobank.GyEmployee;
    import com.zl.tzdb.entity.custom.KhClientlnformation;/**
     * KhRiskevualtion entity. @author MyEclipse Persistence Tools
     */
    @Entity
    @Table(name = "kh_riskevualtion", catalog = "guarantee")
    public class KhRiskevualtion implements java.io.Serializable { // Fields private Integer fxpgId;
    private KhSurvey khSurvey;
    private KhClientlnformation khClientlnformation;
    private GyEmployee gyEmployee;
    private KhRisktype khRisktype;
    private String dccontent;
    private String dcyj;
    private Date dctime; // Constructors /** default constructor */
    public KhRiskevualtion() {
    } /** minimal constructor */
    public KhRiskevualtion(Integer fxpgId) {
    this.fxpgId = fxpgId;
    } /** full constructor */
    public KhRiskevualtion(Integer fxpgId, KhSurvey khSurvey,
    KhClientlnformation khClientlnformation, GyEmployee gyEmployee,
    KhRisktype khRisktype, String dccontent, String dcyj,
    Date dctime) {
    this.fxpgId = fxpgId;
    this.khSurvey = khSurvey;
    this.khClientlnformation = khClientlnformation;
    this.gyEmployee = gyEmployee;
    this.khRisktype = khRisktype;
    this.dccontent = dccontent;
    this.dcyj = dcyj;
    this.dctime = dctime;
    } // Property accessors
    @Id
    @Column(name = "fxpgId", unique = true, nullable = false)
    public Integer getFxpgId() {
    return this.fxpgId;
    } public void setFxpgId(Integer fxpgId) {
    this.fxpgId = fxpgId;
    } @ManyToOne
    @JoinColumn(name = "dcmdId")
    public KhSurvey getKhSurvey() {
    return this.khSurvey;
    } public void setKhSurvey(KhSurvey khSurvey) {
    this.khSurvey = khSurvey;
    } @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name = "customId")
    public KhClientlnformation getKhClientlnformation() {
    return this.khClientlnformation;
    } public void setKhClientlnformation(KhClientlnformation khClientlnformation) {
    this.khClientlnformation = khClientlnformation;
    } @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name = "emp_Id")
    public GyEmployee getGyEmployee() {
    return this.gyEmployee;
    } public void setGyEmployee(GyEmployee gyEmployee) {
    this.gyEmployee = gyEmployee;
    } @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name = "fxflId")
    public KhRisktype getKhRisktype() {
    return this.khRisktype;
    } public void setKhRisktype(KhRisktype khRisktype) {
    this.khRisktype = khRisktype;
    } @Column(name = "dccontent", length = 65535)
    public String getDccontent() {
    return this.dccontent;
    } public void setDccontent(String dccontent) {
    this.dccontent = dccontent;
    } @Column(name = "dcyj", length = 100)
    public String getDcyj() {
    return this.dcyj;
    } public void setDcyj(String dcyj) {
    this.dcyj = dcyj;
    } @Column(name = "dctime", length = 19)
    public Date getDctime() {
    return this.dctime;
    } public void setDctime(Date dctime) {
    this.dctime = dctime;
    }}
      

  3.   

    我遇到过这个问题。我当时是。一对多查找出来,,,输出到JSP页面但是输出之前SESSION已经关闭了..所以导致这个错误可以在MANYTOONE上设置lazy="false"..
      

  4.   

    没有在web.xml中配置openSessionInView这个过滤器
      

  5.   


    在annotation怎么设置lazy=“false” ???
      

  6.   

    你用了延迟加载,输出到JSP页面时,没有在web.xml配置OpenSessionInViewFilter,
    记得在struts之前配置,否则没用
      

  7.   

    你的代码并没有错,只是你的项目已经是在另一台机器上运行过了, 而到你的机器上就会报错,这个错误很常见,第一可能因为是版本不一样,第二你把之前的jar包全部删除了,是重新附加的jar包,是不是啊??要是这样的话,只要一个简单的方法就不会报这种错误了,通过在myEclipse中project-->Clean all projects重新在部署运行就不会报这个错误了
      

  8.   

    web.xml中配置openSessionInView这个过滤器,或者在一对多那里把lazy="false"
      

  9.   

    manytoone注解里面应该有lazy这个配置项配成false
      

  10.   


    没有 jar包还是原来的jar包...........
      

  11.   


    manytoone注解里 只有这两项 fetch=FetchType.LAZZ 和 fetch=FetchType.EVAGE
      

  12.   

    楼主 你把你事务的配置方法发过来
    这个地方说的是你的延迟加载器加载失败 没有找到会话
    应该是你没有开事务回话如果你在用Spring管理事务的话 那么你要看看这个调用的资源是否已经在Spring配置了如果你是在用手动事务的话一定要保证查询方法在事务范围内 因为手动管理事务的时候 在请求延迟加载的时候很可能你的事务已经提交把该会话关闭了
      

  13.   

    解决此类的问题办法总结
    1,hibernate.initialize()方法对延迟加载的对象进行初始化
    2,设置lazy=false
    3,openSessionInView,见一个filter(过滤器)当请求到来时创建session,然后执行chain.dofilter方法后(显示页面后),把session关闭.注意:之前的创建session和关闭session应该删去