cancel
Showing results for 
Search instead for 
Did you mean: 

Hibernate Lazy initialization error in TaskListeners

bcelikcan
Champ in-the-making
Champ in-the-making
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);
   }
}

3 REPLIES 3

bcelikcan
Champ in-the-making
Champ in-the-making
Any Suggestions?

ronald_van_kuij
Champ on-the-rise
Champ on-the-rise
different use of transactions, different 'scope' of hibernate sessions etc…? Without more detail it is hard to say.

bcelikcan
Champ in-the-making
Champ in-the-making
Hi Ronald,

I can provide any details you need.

Thanks.