I have a Spring application. After login, I am invoking getUserByEmail() method.
I only need user and role data. Based on role I am going to display different views, Each view has different data and requiring different child entity of User.
Seems like I have to call getUserByEmail() with different child entity.
This is my partial code involving Entities:
EntityGraph(value = "withAddresses", type = EntityGraphType.FETCH)
public class User{
public firstName;
public lastName;
@OneToMany(fetch = FetchType.LAZY)
public List<Address> addresses
@OneToMany(fetch = FetchType.LAZY)
public List<Order> orders;
}
public userRepository extend jPaRepository(User.class,Long){
@EntityGraph(name="withAdresses")
getUserByEmail(String email)
/* if possible */
@EntityGraph(name="withOrder")
getUserByEmail(String email)
}
Is it possible to have two graph of User objects with same query name? Because I need different data for different views.
Also, when switching to a new view (new call in spring controller), transaction from previous view will be closed already and I have to make new call to have different data with user. I don't understand how fetch lazy is helpful if you are not in same transaction service method, unless I am not missing something.
For example if I need order data in "orderWiew.html" lazy load of order is not going to help I have to make another full call to same user data and additional Order data