You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by jp...@mchsi.com on 2009/06/25 16:23:32 UTC

Hibernate LazyInitializationException

I am getting an "org.hibernate.LazyInitializationException - could not initialize proxy - no Session" exception when I try to load an object stored in session. I understand but I am not certain about how to fix it with Wicket. In reviewing the Wicket In Action book, it looks like the way to handle this is to use a LoadableDetachableModel, which I tried. The model is as follows:

public class DetachableUserModel extends LoadableDetachableModel<EzdecUser> {

    @SpringBean
    private ISecurityService securityService;
    
    private final String email;

    public DetachableUserModel(EzdecUser u) {
        this(u.getEmail());
    }

    public DetachableUserModel(String email) {
        if (email == null) {
            throw new IllegalArgumentException();
        }
        this.email = email;
        System.out.println("email is " + email);
        InjectorHolder.getInjector().inject(this);
    }

    @Override
    public int hashCode() {
        return email.hashCode();
    }

    @Override
    public boolean equals(final Object obj) {
        if (obj == this) {
            return true;
        } else if (obj == null) {
            return false;
        } else if (obj instanceof DetachableUserModel) {
            DetachableUserModel other = (DetachableUserModel) obj;
            return email.equals(other.email);
        }
        return false;
    }

    @Override
    protected EzdecUser load() {
        EzdecUser u = securityService.findUserByEmail(email);
        return u;
    }

}

The relevant section of the code where I am using the model is as follows:

 public UpdateUserProfilePage() {
        this(EzdecSession.getCurrentUser());
    }

    public UpdateUserProfilePage(EzdecUser user) {
        this(new DetachableUserModel(user));
    }

    private UpdateUserProfilePage(DetachableUserModel userModel) {
        this.user = (EzdecUser)userModel.getObject();
        setup();
    }

Anyone have any suggestions on how I can fix this?


Re: Hibernate LazyInitializationException

Posted by Neil Curzon <ne...@gmail.com>.
How do you initialize your components? I think you want to give your labels
etc PropertyModels on the LoadableDetachableModel, so with every new
request, a fresh model object will be grabbed from your service.

Does the page work correctly in some circumstances? If not, you may have
issues with your session or transaction demarcation. The session should
still be active while wicket is rendering the page and components.

On Thu, Jun 25, 2009 at 10:55 AM, jpalmer1026 <jp...@mchsi.com> wrote:

>
> How do you recommend holding the model in the page? In other words, what is
> the alternative code to  "this.user = (EzdecUser)userModel.getObject();"?
>
>
> Martin Sachs wrote:
> >
> > hi
> >
> > you used   "this.user = (EzdecUser)userModel.getObject();" This would
> > hold in page and not initialized on next request.
> > You have to hold the model in the page.
> >
> > We have also found an alternative way. You can write a simple Aspect
> > with AspectJ to reinit all proxies. This can easily done with
> > hibernateSession.lock(NONE).
> > Create pointcuts to each getter which is returning a proxy and init the
> > proxies.
> >
> > Martin
> >
> >
> >
> >
> > jpalmer1026@mchsi.com schrieb:
> >> I am getting an "org.hibernate.LazyInitializationException - could not
> >> initialize proxy - no Session" exception when I try to load an object
> >> stored in session. I understand but I am not certain about how to fix
> >> it with Wicket. In reviewing the Wicket In Action book, it looks like
> >> the way to handle this is to use a LoadableDetachableModel, which I
> >> tried. The model is as follows:
> >>
> >> public class DetachableUserModel extends
> >> LoadableDetachableModel<EzdecUser> {
> >>
> >>     @SpringBean
> >>     private ISecurityService securityService;
> >>
> >>     private final String email;
> >>
> >>     public DetachableUserModel(EzdecUser u) {
> >>         this(u.getEmail());
> >>     }
> >>
> >>     public DetachableUserModel(String email) {
> >>         if (email == null) {
> >>             throw new IllegalArgumentException();
> >>         }
> >>         this.email = email;
> >>         System.out.println("email is " + email);
> >>         InjectorHolder.getInjector().inject(this);
> >>     }
> >>
> >>     @Override
> >>     public int hashCode() {
> >>         return email.hashCode();
> >>     }
> >>
> >>     @Override
> >>     public boolean equals(final Object obj) {
> >>         if (obj == this) {
> >>             return true;
> >>         } else if (obj == null) {
> >>             return false;
> >>         } else if (obj instanceof DetachableUserModel) {
> >>             DetachableUserModel other = (DetachableUserModel) obj;
> >>             return email.equals(other.email);
> >>         }
> >>         return false;
> >>     }
> >>
> >>     @Override
> >>     protected EzdecUser load() {
> >>         EzdecUser u = securityService.findUserByEmail(email);
> >>         return u;
> >>     }
> >>
> >> }
> >>
> >> The relevant section of the code where I am using the model is as
> >> follows:
> >>
> >>  public UpdateUserProfilePage() {
> >>         this(EzdecSession.getCurrentUser());
> >>     }
> >>
> >>     public UpdateUserProfilePage(EzdecUser user) {
> >>         this(new DetachableUserModel(user));
> >>     }
> >>
> >>     private UpdateUserProfilePage(DetachableUserModel userModel) {
> >>         this.user = (EzdecUser)userModel.getObject();
> >>         setup();
> >>     }
> >>
> >> Anyone have any suggestions on how I can fix this?
> >>
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> > For additional commands, e-mail: users-help@wicket.apache.org
> >
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/Hibernate-LazyInitializationException-tp24204249p24204789.html
> Sent from the Wicket - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: Hibernate LazyInitializationException

Posted by jpalmer1026 <jp...@mchsi.com>.
How do you recommend holding the model in the page? In other words, what is
the alternative code to  "this.user = (EzdecUser)userModel.getObject();"?


Martin Sachs wrote:
> 
> hi
> 
> you used   "this.user = (EzdecUser)userModel.getObject();" This would
> hold in page and not initialized on next request.
> You have to hold the model in the page.
> 
> We have also found an alternative way. You can write a simple Aspect
> with AspectJ to reinit all proxies. This can easily done with
> hibernateSession.lock(NONE).
> Create pointcuts to each getter which is returning a proxy and init the
> proxies.
> 
> Martin
> 
> 
> 
> 
> jpalmer1026@mchsi.com schrieb:
>> I am getting an "org.hibernate.LazyInitializationException - could not
>> initialize proxy - no Session" exception when I try to load an object
>> stored in session. I understand but I am not certain about how to fix
>> it with Wicket. In reviewing the Wicket In Action book, it looks like
>> the way to handle this is to use a LoadableDetachableModel, which I
>> tried. The model is as follows:
>>
>> public class DetachableUserModel extends
>> LoadableDetachableModel<EzdecUser> {
>>
>>     @SpringBean
>>     private ISecurityService securityService;
>>    
>>     private final String email;
>>
>>     public DetachableUserModel(EzdecUser u) {
>>         this(u.getEmail());
>>     }
>>
>>     public DetachableUserModel(String email) {
>>         if (email == null) {
>>             throw new IllegalArgumentException();
>>         }
>>         this.email = email;
>>         System.out.println("email is " + email);
>>         InjectorHolder.getInjector().inject(this);
>>     }
>>
>>     @Override
>>     public int hashCode() {
>>         return email.hashCode();
>>     }
>>
>>     @Override
>>     public boolean equals(final Object obj) {
>>         if (obj == this) {
>>             return true;
>>         } else if (obj == null) {
>>             return false;
>>         } else if (obj instanceof DetachableUserModel) {
>>             DetachableUserModel other = (DetachableUserModel) obj;
>>             return email.equals(other.email);
>>         }
>>         return false;
>>     }
>>
>>     @Override
>>     protected EzdecUser load() {
>>         EzdecUser u = securityService.findUserByEmail(email);
>>         return u;
>>     }
>>
>> }
>>
>> The relevant section of the code where I am using the model is as
>> follows:
>>
>>  public UpdateUserProfilePage() {
>>         this(EzdecSession.getCurrentUser());
>>     }
>>
>>     public UpdateUserProfilePage(EzdecUser user) {
>>         this(new DetachableUserModel(user));
>>     }
>>
>>     private UpdateUserProfilePage(DetachableUserModel userModel) {
>>         this.user = (EzdecUser)userModel.getObject();
>>         setup();
>>     }
>>
>> Anyone have any suggestions on how I can fix this?
>>
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Hibernate-LazyInitializationException-tp24204249p24204789.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Hibernate LazyInitializationException

Posted by Martin Sachs <sa...@gmail.com>.
hi

you used   "this.user = (EzdecUser)userModel.getObject();" This would
hold in page and not initialized on next request.
You have to hold the model in the page.

We have also found an alternative way. You can write a simple Aspect
with AspectJ to reinit all proxies. This can easily done with
hibernateSession.lock(NONE).
Create pointcuts to each getter which is returning a proxy and init the
proxies.

Martin




jpalmer1026@mchsi.com schrieb:
> I am getting an "org.hibernate.LazyInitializationException - could not
> initialize proxy - no Session" exception when I try to load an object
> stored in session. I understand but I am not certain about how to fix
> it with Wicket. In reviewing the Wicket In Action book, it looks like
> the way to handle this is to use a LoadableDetachableModel, which I
> tried. The model is as follows:
>
> public class DetachableUserModel extends
> LoadableDetachableModel<EzdecUser> {
>
>     @SpringBean
>     private ISecurityService securityService;
>    
>     private final String email;
>
>     public DetachableUserModel(EzdecUser u) {
>         this(u.getEmail());
>     }
>
>     public DetachableUserModel(String email) {
>         if (email == null) {
>             throw new IllegalArgumentException();
>         }
>         this.email = email;
>         System.out.println("email is " + email);
>         InjectorHolder.getInjector().inject(this);
>     }
>
>     @Override
>     public int hashCode() {
>         return email.hashCode();
>     }
>
>     @Override
>     public boolean equals(final Object obj) {
>         if (obj == this) {
>             return true;
>         } else if (obj == null) {
>             return false;
>         } else if (obj instanceof DetachableUserModel) {
>             DetachableUserModel other = (DetachableUserModel) obj;
>             return email.equals(other.email);
>         }
>         return false;
>     }
>
>     @Override
>     protected EzdecUser load() {
>         EzdecUser u = securityService.findUserByEmail(email);
>         return u;
>     }
>
> }
>
> The relevant section of the code where I am using the model is as follows:
>
>  public UpdateUserProfilePage() {
>         this(EzdecSession.getCurrentUser());
>     }
>
>     public UpdateUserProfilePage(EzdecUser user) {
>         this(new DetachableUserModel(user));
>     }
>
>     private UpdateUserProfilePage(DetachableUserModel userModel) {
>         this.user = (EzdecUser)userModel.getObject();
>         setup();
>     }
>
> Anyone have any suggestions on how I can fix this?
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org