How avoid taking an object from a query, beyond a certain depth, in java-ee?

49 Views Asked by At

I'm trying to implement the 'following mechanism' in my Java EE application. I have an Entity Profile that contain, among other things, a List, which is the list of the profiles that the user follows. Le relation is (@OneToMany).

Everithing is fine and sounds great, but i have a problem. A query like that:

Query q = em.createQuery("SELECT p FROM Profile p WHERE p.id =:custId");

Return a Profile p, and so far, no problem. But I noticed that i have the complete access of my following users, and of the following of all my following users, and so on. I see no way out.

I could replace the the List in an array that contain only the id, but only if it is the only choice. Any ideas? Can I "stop" the query by not let her take the Following list of every my Following users ?

1

There are 1 best solutions below

0
On

I think you need to refactor your API. Don't store the followers list in the profile class, create a service e.g ProfileService and call it whenever you want to access the list of followers for a specific profile.

public class ProfileService {

    public List<Profile> getFollowers(Profile profile) {
       Query q = em.createQuery("SELECT p FROM Profile p WHERE p.id =:custId");
       .....
    }
}