高手进来看看java异常

来源:百度知道 编辑:UC知道 时间:2024/06/21 05:15:19
javax.servlet.ServletException: Illegal attempt to associate a collection with two open sessions

CustomerDAOImple类
public void updateCustomer(Customer customer) throws Exception
{
this.getSession().update(customer) ;
this.getSession().beginTransaction().commit() ;
}

public Customer queryById(int id) throws Exception
{
Customer customer = null ;
String hql = "FROM Customer as c WHERE c.id=?" ;
Query query = this.getSession().createQuery(hql) ;
query.setInteger(0, id) ;
int all = query.list().size() ;
if(all>0)
{
customer = (Customer) query.list().get(0) ;
}
return customer ;
}
Struts文件:
public ActionForward update(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception
{
CustomerForm customerForm = (CustomerForm) form ;

int id = Integer

CustomerDAOImple类
public void updateCustomer(Customer customer) throws Exception
{
Transaction tx = this.getSession().beginTransaction();
tx.begin();
this.getSession().update(customer) ;
tx.commit() ;
}
你的update方法并没有添加到事务中去吧
错误提示说你有2个session,说明你的session有一个没有关闭。你在query方法中添加关闭session的语句看看。

Illegal attempt to associate a collection with two open sessions

一个集合,被2个session操作了。你启动事务吧。看楼上那位大哥的建议

应该是使用session后没有关闭,也可能是级联操作有问题

你没有及时关闭session造成的