java - Hibernate OneToMany bi-directional relation is slow -


i have sessions , users classes following bi-directional onetomanymapping(generated 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 code creates new session 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 , therefore last line slow.

how can link session user without fetching whole sessions collection ?

have @ hibernate's extra-lazy loading feature. enable annotating collection @lazycollection(lazycollectionoption.extra). there this example at, , there's set used in this one.

public class users {     @onetomany(fetch=fetchtype.lazy, mappedby="users")     @lazycollection(lazycollectionoption.extra)     public set<sessions> getsessionses() {         return this.sessionses;     } } 

Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -