I have the Sessions and Users classes with the following bi-directional OneToMany (generated using the sleep mode back-processing 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 that creates a new session for the user:
Session s = ...; Users user = (Users) s.createCriteria(Users.class) ..uniqueResult(); Sessions userSession = new Sessions(); userSession.setUsers(user); s.save(userSession); user.getSessionses().add(userSession);
The user has 2k sessions, so the last line is very slow.
How can I associate a session with a user without getting a whole collection of sessions?
source share