0

Inside the following controller

   @Transactional
   @PostMapping(PATH_VARIABLE)
   public void assignStuff(
         @PathVariable final long someNo,
         @PathVariable final long someId)
         throws AccessDeniedException, ValidationException, ResourceNotFoundException
   {
      final Set<SomeEntity> createdEntities = someService.assignSomething(
            resolveData(someNo, someId)
      );

      if (CollectionUtils.isNotEmpty(createdEntties))
      {
         anotherService.publishPackageDataChangeEvent(someNo);
      }
   }

the following exception is thrown

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: someEntity, could not initialize proxy - no Session
    at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:582)
    at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:201)
    at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:561)
    at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:132)
    at org.hibernate.collection.internal.PersistentSet.iterator(PersistentSet.java:163)
    at java.util.Spliterators$IteratorSpliterator.estimateSize(Spliterators.java:1821)
    at java.util.Spliterator.getExactSizeIfKnown(Spliterator.java:408)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
    at aThirdService.thingsChanged(SomeCalculator.java:61)

From other questions, it seems like this should be fixed with @Transactional, right? So why does it still occur?

serv-inc
  • 35,772
  • 9
  • 166
  • 188
  • 2
    Did you check the import of Transactional ? Are you calling the Method from a Rest Controller or a Scheduler ? Are you calling the public Method from outside the class or inside the class? – GJohannes Jun 29 '22 at 08:48
  • @GJohannes: it's a spring controller, so it's called from the outside. The import is `import org.springframework.transaction.annotation.Transactional;` – serv-inc Jun 29 '22 at 09:06
  • 1
    Did you add `@EnableTransactionManagement`? – Nikolai Shevchenko Jun 29 '22 at 09:21
  • @NikolaiShevchenko : yes, `@Transactional` works in general. – serv-inc Jun 29 '22 at 11:38
  • 1
    What does `resolveData` do? And what does `assignSomething` do? – Jens Schauder Jun 29 '22 at 12:05
  • @JensSchauder: `resolveData` is some simple computation. `assignSomething` goes through the very deep entity structure to fetch values and also save them to the database. – serv-inc Jun 29 '22 at 12:21

0 Answers0