0

i am having issues while trying to get Set<> Collection from the DataBase. this is the relevant part of the code:

@Entity

public Class User { 
    ... 

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private Set<User> userFriends;
    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private Set<FriendRequest> friendRequests;
    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private Set<FriendRequest> sentRequests;
}

i tried to change the cascade to CascadeType.Persist and still having the same problem. if anybody knows the problem i would also be happy to understand what i've been doing wrong and the difference between the CascadeTypes(at least when to use each one).

this is the StackTrace i am getting :

Exception in thread "main" java.lang.StackOverflowError
    at java.util.HashMap$KeyIterator.<init>(Unknown Source)
    at java.util.HashMap$KeySet.iterator(Unknown Source)
    at java.util.HashSet.iterator(Unknown Source)
    at java.util.AbstractCollection.toString(Unknown Source)
    at org.hibernate.collection.internal.PersistentSet.toString(PersistentSet.java:328)
    at java.lang.String.valueOf(Unknown Source)
    at java.lang.StringBuilder.append(Unknown Source)
    at MyRally1.demoRally.beans.User.toString(User.java:191)
    at java.lang.String.valueOf(Unknown Source)
    at java.lang.StringBuilder.append(Unknown Source)
    at java.util.AbstractCollection.toString(Unknown Source)
    at org.hibernate.collection.internal.PersistentSet.toString(PersistentSet.java:328)

Thank you !

  • Can you add your stacktrace to the post? And the relevant part where you are trying to fetch records – jetro157 May 04 '20 at 18:56
  • You have a recursive call somewhere. What happens in your method "MyRally1.demoRally.beans.User.toString(User.java:191)"? – TomStroemer May 05 '20 at 09:59
  • i think the problem is that the Set<> of userFriends contains User Entity, and each one of them contains another Set<> of users (userFriends) and so on, and i think this is the recursive call you mentioned.. i remember working on different project and using @JsonIgnore on the Set<>, but nothing helps here. – user3469465 May 05 '20 at 13:20

0 Answers0