Do you have OpenSessionInViewFilter in your web.xml? If no, you will get this
exception whenever you try to do lazy loading outside your DAOs.
add to web.xml:
<filter>
<filter-name>OpenSessionFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>hibernateSessionFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>OpenSessionFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
I always use a self-written class called PersistentObjectModel (a
LoadableDetachableModel) and let all my Persisent classes implement an
interface called IPersistentObject with a single method: Serializable
getId()
public class PersistentObjectModel<T extends IPersistentObject> extends
LoadableDetachableModel {
private static final long serialVersionUID = 1L;
private final Class<T> _clazz;
private final int _id;
@SpringBean(name = "myDao")
private IMyDao _myDao;
@SuppressWarnings("unchecked")
public PersistentObjectModel(final T object) {
super(object);
if (object instanceof HibernateProxy) {
_clazz = (Class<T>) ((HibernateProxy)
object).getHibernateLazyInitializer()
.getImplementation().getClass();
} else {
_clazz = (Class<T>) object.getClass();
}
_id = object.getId();
// inject the bean
InjectorHolder.getInjector().inject(this);
}
public PersistentObjectModel(final Class<T> clazz, final int id) {
_clazz = clazz;
_id = id;
// inject the bean
InjectorHolder.getInjector().inject(this);
}
@Override
protected T load() {
return _myDao.getById(_clazz, _id);
}
@SuppressWarnings("unchecked")
@Override
public T getObject() {
return (T) super.getObject();
}
}
This makes working with hibernate objects really easy:
setModel(new PersistentObjectModel(Foo.class, 1)); // option 1: class and id
setModel(new PersistentObjectModel(foo)); // option 2: persistent object
-----
-------
Stefan Fußenegger
http://talk-on-tech.blogspot.com // looking for a nicer domain ;)
--
View this message in context:
http://www.nabble.com/How-to-avoid-Lazy-loading-exception-tp17040941p17045620.html
Sent from the Wicket - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]