Struts+Hibernate+Spring出现异常错误,高手指点,谢谢!

05-12-01 mhbjava
Struts+Hibernate+Spring出现异常错误,高手指点,谢谢!

依赖注入配置省了
出现的异常为:Failed to lazily initialize a collection - no session or session was closed
谢谢!

QueryManager.java类
public interface QueryManager {
public List find( String query );
}

HibernateQueryManager.java类:
public class HibernateQueryManager extends HibernateDaoSupport implements QueryManager
{
public List find( String query ) {
return getHibernateTemplate().find( query );

}
}

ContactService.java
public interface ContactService
{
public List findContactsByUser( String userName ) ;
}

ContactService.java的实现类ContactServiceImpl.java
public class ContactServiceImpl extends BaseService implements ContactService
{
public List findContactsByUser( String userName )
{
User user = _userService.findUserByName( userName );
String sql = "from Contact as contact where contact.fromUser.id = '" + user.getId().toString() + "' order by contact.id desc";

return qm.find( sql );
}
}

UserService.java
public interface UserService
{
public User findUserByName( String name ) ;
}

UserService.java的实现类UserServiceImpl.java
public class UsersServiceImpl extends BaseService implements UserService
{
public User findUserByName( String name )
{
List list = qm.find( "from User where username= ? ", name );
if( list.size() > 0 ) return (User)list.get(0);
return null;
}
}

action类接受页面传递过来的数据
AlbumAction.java

public class AlbumAction extends BaseAction
{
public ActionForward addAdmin(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws Exception {

String username = (String) request.getParameter("user");
User user = _userService.findUserByName( username );
request.setAttribute( "user", user );
request.setAttribute( "isOwner", isOwner( username ) );
List listFriends = _contactService.findContactsByUser(username); //跟踪调试发现listFriends.size()>=1(即是有数据的,数据可以查询出来)
System.out.println("ok");
request.setAttribute("listFriends",listFriends); //在这里出现错误:请高手指点 Failed to lazily initialize a collection - no session or session was closed
System.out.println("test");
return mapping.findForward("addAdmin");

}
}


以上类之间的关系:

页面------>AlbumAction------>ContactServiceImpl---------->UserServiceImpl
---------->HibernateQueryManager


出现的异常为:
javax.servlet.ServletException: Failed to lazily initialize a collection - no session or session was closed
net.sf.acegisecurity.intercept.web.SecurityEnforcementFilter.doFilter(SecurityEnforcementFilter.java:214)
net.sf.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:311)
net.sf.acegisecurity.ui.basicauth.BasicProcessingFilter.doFilter(BasicProcessingFilter.java:212)
net.sf.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:311)
net.sf.acegisecurity.providers.anonymous.AnonymousProcessingFilter.doFilter(AnonymousProcessingFilter.java:153)
net.sf.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:311)
net.sf.acegisecurity.ui.rememberme.RememberMeProcessingFilter.doFilter(RememberMeProcessingFilter.java:114)
net.sf.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:311)
net.sf.acegisecurity.ui.AbstractProcessingFilter.doFilter(AbstractProcessingFilter.java:374)
net.sf.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:311)
net.sf.acegisecurity.context.HttpSessionContextIntegrationFilter.doFilter(HttpSessionContextIntegrationFilter.java:225)
net.sf.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:311)
net.sf.acegisecurity.util.FilterChainProxy.doFilter(FilterChainProxy.java:179)
net.sf.acegisecurity.util.FilterToBeanProxy.doFilter(FilterToBeanProxy.java:125)
dragonchant.framework.web.EncodingFilter.doFilter(EncodingFilter.java:37)

banq
2005-12-02 11:12
你要保存这个listFriends对象,hibernate会初始化它,但是此时你的hibernate session已经关闭。所以 session开启和关闭一般在sql语句操作前后,不能分离。

猜你喜欢