Spring + Hibernate : LazyInitializationException -


i getting lazyinitializationexception when try retrieve information inside pojo.

user.java

public class user  implements java.io.serializable {   private set groups = new hashset(0);      public set getgroups() {        return this.groups;    }  } 

usercontroller.java

@requestmapping(value = "/home", method = requestmethod.get) public modelandview gethome(httpservletrequest request) throws exception {     modelandview mv;     user user = sessionutil.getsessionuser(request);     if (user == null) {         mv = new modelandview("redirect:/user/login");     } else {         mv = new modelandview("home");         user = this.userservice.getuserbyid(user.getid());          // exception here          set<group> groups = user.getgroups();         mv.addobject("groups", groups);          // work fine         list<group> invitation_groups = this.userservice.getinvitationgroups(user);         mv.addobject("invitation_groups", invitation_groups);          // work fine         list<group> subscription_groups = this.userservice.getsubscriptiongroups(user);         mv.addobject("subscription_groups", subscription_groups);      }      return mv; } 

database

=====
-user-
id
login

=====
-goup-
id
user (foreign key user)


at org.hibernate.proxy.abstractlazyinitializer.getimplementation(abstractlazyinitializer.java:285) @ org.hibernate.proxy.pojo.javassist.javassistlazyinitializer.invoke(javassistlazyinitializer.java:185) @ model.pojo.user_$$_jvst464_2.getgroups(user_$$_jvst464_2.java) @ controller.usercontroller.gethome(usercontroller.java:151) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) 

i think understand why exception : close hibernatesession after transaction in dao session can't open pojo request.

in other hand user.getlogin() exemple works. think dont understand problem is. because uses foreign key ?

i think found workaround here dont know how implement , if it's efficient.

i know if remove session.close() dao works it's not solution.

i hope can me. thanks.

solution

  1. remove hand made transactions
  2. add transactionnal annotation
  3. user opensessioninview filter.

thanks guys.

why handling session manually? need that?

if not, should use opensessioninview pattern. keep session open until request ends, but, careful, can run in trouble lots of queries made database because lazy load of collections. whenever can, try fetch data eagerly if know used.

your user.getlogin() returns string right? if 1 side of relationship mapping, fetched eagerly default.

i'm not used spring think spring has opensessioninview filter manage session.


Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -