Spring boot application using Set<> persistentSet exception

394 Views Asked by At

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 !

0

There are 0 best solutions below