1

Sample code to represent my problem.

Let's say there are entity classes like that

public class User {
    ...
    @ManyToOne
    @JoinColumn(name = "users_statuts_id", referencedColumnName = "id", nullable = false)
    private UserStatus status;

}
public class UserStatus {
    ...
    @OneToMany(mappedBy = "status")
    private List<User> users = new LinkedList<>();

}

This works as expected but, problem starts when I want to use MapStruct generated mapper and DTO object. I will get StackOverflowError becouse of Cyclic references User->UserStatus->List< User>-> ..... And take into account that not necessary it will be in pattern A->B->A (User->UserStatus->User) sometimes it will be User->ClassA->ClassB->...->User

I tired adding Context (CycleAvoidingMappingContext) to mapper as stated in other threads to break cycle but I failed and still get StackOverflowError. Tried too with AfterMappings and Mapping(ignore) but still not worked and I would like to avoid settings manually nulls to break the cycle.

I wonder if is possible to break cycle similar to @JsonIdentityInfo but on Hibernate level ?

My classes are a bit more complicated and bidirectional relation would help a lot later. Someone have some tips how should I make it done to work properly ?

Rafał
  • 9
  • 2
  • Does this answer your question? [Prevent Cyclic references when converting with MapStruct](https://stackoverflow.com/questions/36223752/prevent-cyclic-references-when-converting-with-mapstruct) – xerx593 Oct 06 '22 at 23:08

0 Answers0