It\'s my first question here on stack, so please be gentle :D
I\'m trying to create hibernate OneToMany relationship. When I try to fetch some data from my DB, I\'m
Also see: Hibernate throws StackOverflowError when querying
In my case, I was using the @IdClass annotation on an entity which had a foreign key as part of its primary key:
@IdClass(MyEntity.MyEntityKey.class)
public class MyEntity {
public static class MyEntityKey {
private Foo parent;
private int count;
// Getters, setters, constructor, ...
}
@Id
private Foo parent;
@Id
private int number;
//...
}
Now when loading Foo and eagerly loading all MyEntitys, Hibernate was again loading the parent entity Foo while building the key which resulted in the stack overflow.
I fixed the issue by modelling the id using explicit key properties (i.e. including the keys of Foo in MyEntityKey rather than just a reference to Foo) as shown in Composite key handling, using @Idclass annotation in Spring boot java (also applied to Non-Spring Hibernate). In short: Use the @JoinColumns annotation to specify the relationship between the navigation property (to parent) and the id columns.