The model is that a user can have many LoginSessions (LogginSession can be related to only one user).
What I am trying to achieve is bring the user with related LoginSession (which's token provided in the query).
All of that I am trying to do with only one query, and to do so I want to use eager loading, here is my code:
I have this code:
LoginSession loginSession = new LoginSession();
loginSession.setToken(sessionToken);
//Example loginSessionExample = Example.create(loginSession);
Criteria crit = session.createCriteria(User.class);
crit.createAlias("userLoginSession", "session");
crit.add(Restrictions.eq("session.token", sessionToken));
crit.setMaxResults(1);
crit.setFirstResult(0);
crit.setFetchMode("loginSession", FetchMode.JOIN);
List<?> usersList = (List<?>) crit.list(); // first query
if(usersList.size() == 1)
{
User user = (User) usersList.get(0);
LoginSession loginSession = (LoginSession) user.getUserLoginSession().toArray()[0]; //second query
...
The problem is that there are 2 quires that are being executed (see comments in the provided code).
What am I doing wrong with my Criteria and how can I make to be a one query?
Thanks