T
T
tw19112018-04-02 18:33:39
Java
tw1911, 2018-04-02 18:33:39

Spring Data, LazyInitializationException which pattern to use?

There is a certain Entity class Operation, containing a list of stages objects, marked with the @ManyToMany annotation.
For this class, a repository interface was created inherited from JpaRepository without any additional methods.

public interface OperationTypeRepository extends JpaRepository<OperationType,Long> {
}

There is a CRUD service that implements some business logic using this repository. The read method is implemented elementary
public OperationType read(Long id) {
        return repository.getOne(id);
 }

And finally, there is a controller that uses this service.
OperationType otFromBase = typeCrudService.read(ot1.getId());
otFromBase.getOperStages();

When I try to access stages through a getter, it doesn't matter (FetchType=LAZY or EAGER), I get a LazyInitializationException error. I generally understand what's going on. The question is how to avoid it. There must be some kind of pattern that allows you to resolve this situation.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
aol-nnov, 2018-04-02
@aol-nnov

The good, the bad and the ugly (OSIV pattern) :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question