AnsweredAssumed Answered

Hibernate Lazy initialization error in TaskListeners

Question asked by bcelikcan on Sep 3, 2012
Latest reply on Sep 10, 2012 by bcelikcan
Hi,

We get Hibernate lazy initialization error in task listeners and java delegates in ui class. But it does not occur in our integration tests.

Our project is a spring project. Test environment and ui environment context properties are same.

//Task listener class

public class MyTaskListener extends TaskListener {

    @Override
    public void notify(DelegateTask delegeteTask) {
         
      MyClass myClass = myService.findMyClassById(1L);

                //throw lazy initialize exception
      String myClassProperty = myClass.getMySecondClass().toString();
    }
}

@Entity
public class MyClass {

   @Id
   private Long id;

   @ManyToOne(fetch = FetchType.LAZY)
   private MySecondClass mySecondClass;

   (get set etc …)

}

@Entity
public class MySecondClass {

   private String name;

   @Override
   public String toString() {
      
      return this.name;      
   }
}

//Test class in Test suit
@Transactional
@RunWith(SpringJUnit4ClassRunner.class)
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)
@ContextConfiguration("classpath:applicationcontext-servicetest.xml")
public class BaseIntegration extends AbstractTransactionalJUnit4SpringContextTests {

   @Test
   public void test() {
         
      ProcessService.startProcessInstanceByKey(1L, "test", null);
   }
}

//Spring service in core project
@Service
public class ProcessService {

   @Transactional(rollbackFor = Exception.class)
   public ProcessInstance startProcessInstanceByKey(Long processDefinitionId, String businessKey, Map<String, Object> variables) {

      return ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinitionId, businessKey, variables);

   }
}

//Form class in ui project

public class UiFormClass {

   @Transactional   
   public void startProcess() {
         
      ProcessService.startProcessInstanceByKey(1L, "test", null);
   }
}

Outcomes