I have Sessions
and Users
classes with following bi-directional
OneToMany
mapping(generated with hibernate reverse engineering tool):
public class Users {
@OneToMany(fetch=FetchType.LAZY, mappedBy="users")
public Set<Sessions> getSessionses() {
return this.sessionses;
}
}
public class Sessions {
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="USER_ID")
public Users getUsers() {
return this.users;
}
}
And here is my code which creates new session for user:
Session s = ...;
Users user = (Users) s.createCriteria(Users.class)
./*restrictions...*/.uniqueResult();
Sessions userSession = new Sessions();
userSession.setUsers(user);
s.save(userSession);
user.getSessionses().add(userSession); // here getSessionses() has 2k records
User has 2k sessions and therefore last line is very slow.
How can I link session with user without fetching whole sessions collection ?