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?

Reply via email to