Hibernate: LazyInitializationException

1 resposta
DirceuSobrinho

Oi

Estou usando o hibernate e todas as operações com hibernate esta disparando a seguinte excecao:

Exception in thread "main" org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
eu estou executando a seguinte classe teste:
public static void main(String[] args) throws Exception {

    DAOFactoryHibernate d = DAOFactoryHibernate.getInstance();
    ModeloDAOHibernate dao = (ModeloDAOHibernate) d.makeModeloDAO();
    Modelo modelo = (Modelo) dao.retrieve(3);
    System.out.println(modelo.getNome());
					
}

e estou usando dao, seguese um pedaco da DAOFactoryHibernate:

public class DAOFactoryHibernate implements DAOFactory{

	private SessionFactory sessionfactory;
	private static DAOFactoryHibernate instance;
	
	private DAOFactoryHibernate(){
		try{
		     Configuration config = new Configuration();
		     Properties prop = new Properties();			     prop.load(getClass().getResourceAsStream("map/config.properties"));
		     config.setProperties(prop);
                                     config.configure(getClass().getResource("map/hibernate.cfg.xml"));
		     sessionfactory = config.buildSessionFactory();
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	public ModeloDAO makeModeloDAO(){
		return new ModeloDAOHibernate(sessionfactory);
	}
	
	public static DAOFactoryHibernate getInstance(){
		
		if(instance==null){
			instance = new DAOFactoryHibernate();
		}
		return instance;
	}
}

Att, Dirceu

1 Resposta

J

Você colocou alguma coisa com Lazy Instatiation, e dentro desse retrieve provavelmente você dá um sessao.close(), aí ele não consegue carregar o atributo depois…

Criado 30 de março de 2006
Ultima resposta 31 de mar. de 2006
Respostas 1
Participantes 2