日期:2014-05-20  浏览次数:20871 次

hibernate跟spring整合中session管理的问题
我做的有几个模块,用户管理和模块的管理,在用户管理中并没有出现关于session异常的现象,可是在做模块管理的时候却出现了异常,代码都类似,后来在百度搜好像是关于session的线程管理问题,我的web.xml中已经配置:
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
DAO类中的代码如下:
public void delModule(int moduleId) {

Module module = findById(moduleId);
if(module.getChildren().size()>0)
{
throw new RuntimeException("模块【"+module.getName()+"】存在有子模块,不允许删除");
}

getHibernateTemplate().delete(module);
}


public Module findModule(int moduleId) {

Session session = this.getSession();

Query query = session.createQuery("from Module as m where m.id="+moduleId);

return (Module)query.uniqueResult();

}


public Module findById(int moduleId)
{
Module module=(Module)getHibernateTemplate().load(Module.class, moduleId);

return module;
}

在delModule中,我分别调用findModule(int moduleId),和findById(int moduleId)获取module对象,可是调用时出现的错误分别是:
2009-5-24 12:06:03 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet default threw exception
org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
at org.hibernate.collection.AbstractPersistentCollection.setCurrentSession(AbstractPersistentCollection.java:410)
at org.hibernate.event.def.OnUpdateVisitor.processCollection(OnUpdateVisitor.java:43)
at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:101)
at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:61)
at org.hibernate.event.def.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:55)
at org.hibernate.event.def.AbstractVisitor.process(AbstractVisitor.java:123)
at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:101)
at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:52)
at org.hibernate.impl.SessionImpl.fireDelete(SessionImpl.java:766)
at org.hibernate.impl.SessionImpl.delete(SessionImpl.java:744)
at org.springframework.orm.hibernate3.HibernateTemplate$25.doInHibernate(HibernateTemplate.java:848)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.delete(HibernateTemplate.java:842)
at org.springframework.orm.hibernate3.HibernateTemplate.delete(HibernateTemplate.java:838)
at org.myy.manager.impl.ModuleManager.delModule(ModuleManager.java:27)
at org.myy.action.ModuleManagerAction.delMod(ModuleManagerAction.java:143)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
...




12:08:31,361 ERROR LazyInitializationException:19 - could not initialize proxy - no Session
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:57)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150)