0

I use java batch (jsr-352) in wildfly 9.0.1. Processor of step(chunk-based) access to lazy field of entity. When processor want to fetch lazy field I give LazyInitializationException. When I run it on wildfly 11.0.0.Alpha1 everything works fine. In wildfly 9.0.1 when I fetch this field using repository there is no problem. How can I do that whitout using repository?

reader:

public Object readItem() {
   return answerRepository.findBy(23);
}

processor:

public Object processItem(Object item) {
   AnswerEntitiy answerEntitiy = (AnswerEntitiy)item;
   //when i call answerEntitiy.getComment() LazyInitializationException throws
   return commentRepository.findByAnswer(answerEntitiy); //works fine
}

writer:

public void writeItems(List<Object> items) {
    //loops and casting
    commentRepository.save(comment); //LazyInitializationException
}

Here is complete error:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.test.AnswerEntitiy.comments, could not initialize proxy - no Session
mohammad_1m2
  • 1,571
  • 5
  • 19
  • 38
  • What do you mean by "fetching by using repository"? Did you mean get data with direct jdbc calls from batch job repository? – cheng Jan 29 '18 at 19:10
  • When I use method query there no problem. I added complete example above. – mohammad_1m2 Jan 29 '18 at 19:54
  • Is wildfly 9.0.1 using jberet? – mohammad_1m2 Jan 29 '18 at 22:28
  • Yes, WildFly 9 contains JBeret as its batch subsystem provider. – cheng Jan 29 '18 at 22:34
  • On the surface it looks like a JPA / Hibernate configuration problem. Have you checked the @OneToMany annotation in AnserEntity, probably the collection relationship field to CommentEntity? There you should be able to specify the appropriate fetch type. – cheng Jan 29 '18 at 22:36
  • Yes, I annotated entity and Fetch type is lazy. Assume I don’t use getComment; Why when I call save method in writer, exception throws? thanks. – mohammad_1m2 Jan 29 '18 at 22:56

1 Answers1

0

I assume you use JPA to fetch your entities. Apparently the PersistenceContext that loaded your question is no longer available by the time you access the comment, hence you get that exception.

As a solution, you must load the comment in the same context as the core entity fields. I see these possibilities:

  1. check if you can increase the scope of the persistence context. After all the transactions do span reader, processor and writer for every chunk, maybe something similar is possible for the PersistenceContext.
  2. Even if your entity is declared to be fetched lazily, you can either override that setting for the query, or explicityly load the comment field by accessing it in the reader. This invalidates lazy loading but will definitely work. It can be a penalty if you need the field sometimes only.
  3. The last possibility is to load all additional data in the processor's PersistenceContext, which is what you already evaluated. The advantage is that it definitely works and does not loose the advantages of lazy loading.
Queeg
  • 7,748
  • 1
  • 16
  • 42