无法初始化代理 - 没有会话

时间:2014-02-24 23:32:24

标签: spring hibernate proxy

[ERROR] could not initialize proxy - no Session
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
    at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:57)
    at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
    at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150)
    at com.model.dto.Distination$$EnhancerByCGLIB$$f4a8517c.getLibileDis(<generated>)
    at com.business.impl.MeteobussinesImpl.afficherMeteo1(MeteobussinesImpl.java:30)
    at com.test.Tester.main(Tester.java:32)
[ERROR] could not initialize proxy - no Session
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
    at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:57)
    at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
    at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150)
    at com.model.dto.Distination$$EnhancerByCGLIB$$f4a8517c.toString(<generated>)
    at com.model.dto.Distination$$EnhancerByCGLIB$$f4a8517c.getLibileDis(<generated>)
    at com.business.impl.MeteobussinesImpl.afficherMeteo1(MeteobussinesImpl.java:30)
    at com.test.Tester.main(Tester.java:32)

1.class main

package com.test;

import java.util.Iterator;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.business.Meteobussines;
import com.business.impl.MeteobussinesImpl;
import com.model.vo.Meteo;

public class Tester {
    static public void displayList(List list) 
    { Iterator iter = list.iterator();
    if (!iter.hasNext()) 
    { System.out.println("La lsite est vide"); return; }
    while (iter.hasNext()) { 
     Meteo ct = (Meteo) iter.next();
      System.out.println("tempsMax :" + ct.getTempMax() + " pays :" + ct.getLibilePays() + " distination :" + ct.getLibileDistination()); }}

    public static void main(String[] args) {

        // TODO Auto-generated method stub

        String[] configArray = new String[] { "/com/resource/spring/ApplicationContext.xml",
                "/com/resource/spring/ApplicationContextDao.xml","/com/resource/spring/ApplicationContextBusiness.xml"};

    ApplicationContext ctx = new ClassPathXmlApplicationContext(configArray);

        Meteobussines mete =  (Meteobussines) ctx.getBean("MeteoBuss");
        List<Meteo> m = mete.afficherMeteo1("tounes");
        displayList(m);




    }

}

2.Meteobussiness

package com.business.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.business.Meteobussines;
import com.dao.HistoriqueDao;
import com.dao.impl.HistoriqueDaoImpl;
import com.model.dto.Historique;
import com.model.vo.Meteo;

public class MeteobussinesImpl implements Meteobussines {

     HistoriqueDao historiqueDao  ;

    @SuppressWarnings("null")
    @Override
    public List<Meteo> afficherMeteo1(String pays) {
        List<Historique> hiss= historiqueDao.rechercher(pays);
        List<Meteo> m = new ArrayList<Meteo>(); ;
         Iterator iter = hiss.iterator();
         if (!iter.hasNext()) 
         { System.out.println("La lsite est vide"); }
         while (iter.hasNext()) { 

             Historique ct = (Historique) iter.next();
            Meteo me =new Meteo();
            me.setDateHis(ct.getDateHis());
            me.setLibileDistination(ct.getDistination().getLibileDis());
            me.setLibilePays(ct.getPays().getLibilePays());
            me.setLibileVille(ct.getVille().getLibileVille());
            me.setTempMax(ct.getTempMax());
            me.setTempMin(ct.getTempMin());
            m.add(me);

         }
         return m;
        // TODO Auto-generated method stub

    }

    @Override
    public List<Meteo> afficherMeteo2(String pays, String ville) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<Meteo> afficherMeteo3(String pays, String ville,
            String distination) {
        // TODO Auto-generated method stub
        return null;
    }

    public HistoriqueDao getHistoriqueDao() {
        return historiqueDao;
    }

    public void setHistoriqueDao(HistoriqueDao historiqueDao) {
        this.historiqueDao = historiqueDao;
    }

}

3.HistroiqueDaao

package com.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.Expression;

import com.dao.HistoriqueDao;
import com.model.dto.Historique;

@SuppressWarnings("unchecked")
public class HistoriqueDaoImpl extends GenericDaoImpl implements HistoriqueDao {


    @Override
    public List<Historique> rechercher(String critere1, String critere2,
            String critere3) {
        Criteria    crit = getSession().createCriteria(Historique.class);
        crit.setFetchMode("pays", FetchMode.JOIN);
        crit.createAlias("pays", "p");
        crit.add(Expression.eq("p.libilePays", critere1));
        crit.setFetchMode("ville", FetchMode.JOIN);
        crit.createAlias("ville", "b");
        crit.add(Expression.eq("b.libileVille", critere2));
        crit.setFetchMode("distination", FetchMode.JOIN);
        crit.createAlias("distination", "d");
        crit.add(Expression.eq("d.libileDis", critere3));
        List<Historique> his = crit.list();



        return his;
    }

    @Override
    public List<Historique> rechercher(String critere1, String critere2) {
        // TODO Auto-generated method stub
        Criteria    crit = getSession().createCriteria(Historique.class);
        crit.setFetchMode("pays", FetchMode.JOIN);
        crit.createAlias("pays", "p");
        crit.add(Expression.eq("p.libilePays", critere1));
        crit.setFetchMode("ville", FetchMode.JOIN);
        crit.createAlias("ville", "b");
        crit.add(Expression.eq("b.libileVille", critere2));
        List<Historique> his = crit.list();
        return his;
    }

    @Override
    public List<Historique> rechercher(String critere1) {
        // TODO Auto-generated method stub

        Criteria    crit = getSession().createCriteria(Historique.class);
        crit.setFetchMode("pays", FetchMode.JOIN);
        crit.createAlias("pays", "p");
        crit.add(Expression.eq("p.libilePays", critere1));
        List<Historique> his = crit.list();
        return his;

    }

}

3.i在春天使用DaoSupport初始化

2 个答案:

答案 0 :(得分:0)

堆栈跟踪指向此行:

me.setLibileDistination(ct.getDistination().getLibileDis());

ctHistorique代理=假对象,其字段(包括您通过getDistination()获得的字段)尚未启动。

在提交Hibernate事务之前,请初始化您感兴趣的字段。

Hibernate.initialize(ct.getDistination());

答案 1 :(得分:0)

crit.setFetchMode("pays", FetchMode.JOIN);
crit.setFetchMode("ville", FetchMode.JOIN);
crit.setFetchMode("distination", FetchMode.JOIN);

这个问题的解决方案导致我在所有情况下打开fetchmode.join