Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2013
    Posts
    69
    Rep Power
    0

    Default org.hibernate.LazyInitializationException - could not initialize proxy - no Session

    I get the following exception:
    Exception in thread "main" org.hibernate.LazyInitializationException: could not initialize proxy - no Session
    at org.hibernate.proxy.AbstractLazyInitializer.initia lize(AbstractLazyInitializer.java:167)
    at org.hibernate.proxy.AbstractLazyInitializer.getImp lementation(AbstractLazyInitializer.java:215)
    at org.hibernate.proxy.pojo.javassist.JavassistLazyIn itializer.invoke(JavassistLazyInitializer.java:190 )
    at sei.persistence.wf.entities.Element_$$_jvstc68_47. getNote(Element_$$_jvstc68_47.java)
    at JSON_to_XML.createBpmnRepresantation(JSON_to_XML.j ava:139)
    at JSON_to_XML.main(JSON_to_XML.java:84)


    when I try to call from main the following lines:
    Java Code:
        Model subProcessModel = getModelByModelGroup(1112);
        System.out.println(subProcessModel.getElement().getNote());
    I implemented the
    Java Code:
    getModelByModelGroup(int modelgroupid)
    method firstly like this :
    Java Code:
        	public static Model getModelByModelGroup(int modelGroupId, boolean openTransaction) {
          		
          		Session session = SessionFactoryHelper.getSessionFactory().getCurrentSession();		
          		Transaction tx = null;
          		
          		if (openTransaction)
          			tx = session.getTransaction();
          		
          		String responseMessage = "";
          		
          		try {
          			if (openTransaction)			
          				tx.begin();
          			Query query = session.createQuery("from Model where modelGroup.id = :modelGroupId");
          			query.setParameter("modelGroupId", modelGroupId);
          			@SuppressWarnings("unchecked")
        			List<Model> modelList = (List<Model>)query.list(); 
          			Model model = null;
          			// Cerco il primo Model che  in esercizio: idwf_model_type = 3
          			for (Model m : modelList)
          				if (m.getModelType().getId() == 3) {
          					model = m;
          					break;
          				}
          			
          			if (model == null) {
          				Object[] arrModels = modelList.toArray();
          				if (arrModels.length == 0) 
          					throw new Exception("Non esiste al momento nessuna implementazione del processo.");
          				
          				model = (Model)arrModels[0];
          			}
          			
          			if (openTransaction)
          				tx.commit();
          			return model;
          			
          		} catch(Exception ex) {
          			if (openTransaction)
          				tx.rollback();
          			ex.printStackTrace();
          			if (responseMessage.compareTo("") == 0)
          				responseMessage = "Errore nel recupero del modello:\n" + ex.getMessage();
          			return null;		
          		}
    and got the exception. Then a friend suggested me to always test the session and get the current session to avoid this error. So i did this:
    Java Code:
        public static Model getModelByModelGroup(int modelGroupId) {
          		
          		Session session = null;
          		boolean openSession = session == null;
          		Transaction tx = null;
          		if (openSession){
          		  session = SessionFactoryHelper.getSessionFactory().getCurrentSession();	
          			tx = session.getTransaction();
          		}
          		String responseMessage = "";
          		
          		try {
          			if (openSession)			
          				tx.begin();
          			Query query = session.createQuery("from Model where modelGroup.id = :modelGroupId");
          			query.setParameter("modelGroupId", modelGroupId);
          			@SuppressWarnings("unchecked")
        			List<Model> modelList = (List<Model>)query.list(); 
          			Model model = null;
          			// Cerco il primo Model che  in esercizio: idwf_model_type = 3
          			for (Model m : modelList)
          				if (m.getModelType().getId() == 3) {
          					model = m;
          					break;
          				}
          			
          			if (model == null) {
          				Object[] arrModels = modelList.toArray();
          				if (arrModels.length == 0) 
          					throw new RuntimeException("Non esiste al momento nessuna implementazione del processo.");
          				
          				model = (Model)arrModels[0];
          			
          			if (openSession)
          				tx.commit();
          			return model;
          			
          		} catch(RuntimeException ex) {
          			if (openSession)
          				tx.rollback();
          			ex.printStackTrace();
          			if (responseMessage.compareTo("") == 0)
          				responseMessage = "Errore nel recupero del modello:\n" + ex.getMessage();
          			return null;		
          		}
        
          	}
    but still get the same error.
    I have been reading a lot for this error and found some possible solutions. One of them was to set lazyLoad to false but I am not allowed to do this thats why i was suggested to control the session

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,098
    Rep Power
    6

    Default Re: org.hibernate.LazyInitializationException - could not initialize proxy - no Sessi

    I'm seeing double:

    java - org.hibernate.LazyInitializationException - could not initialize proxy - no Session - Stack Overflow

    You should post links to your crossposts so people can follow them and see what has already been answered, to prevent wasting time doing double effort.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Hibernate - Session use
    By orion_mcl in forum New To Java
    Replies: 0
    Last Post: 10-31-2010, 11:54 PM
  2. Replies: 0
    Last Post: 08-29-2009, 07:32 PM
  3. Replies: 3
    Last Post: 08-01-2007, 04:34 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •