关于hibernate最经典的exception

在一次简单的页面UPDATE之前 我想先查询一遍这个对象

结果出现了javax.servlet.ServletException: a different object with the same identifier value was already associated with the session异常

这个主要是因为在一个页面之中已经有一个持久化对象了,但我又去查了一遍,因此在提交一次修改update请求的时候就会使session中存在着一个相同id的两个不同对象,这样处于了摸棱两可的局面

因此我的解决方法是在update之前的select用一个别的Session

public String updateTfsj() {
  try {
   String[] only = onlyStr.split(",");
   if (onlyStr != null && !onlyStr.equals("")) {
    for (Object o : only) {
     if (o.toString() != null) {
      // System.out.println(o.toString());
      deleteFilesTfsjk(Integer.parseInt(o.toString()));
     }
     ;
    }
   }

   Calendar c = Calendar.getInstance();
   Set fjSet = new HashSet();
   appYuanmoban = (AppYuanmoban) tfsjService.load(AppYuanmoban.class, yaid);
   tfsjk.setYaid(appYuanmoban);

   if (zjId != null) {
    AppPgb appPgb = (AppPgb) tfsjService.load(AppPgb.class, zjId);
    tfsjk.setZjjb(appPgb);
   }
   String[] test = getRs(tfsjk.getId().toString());
   tfsjk.setSjzt(test[0]);
   tfsjk.setXfOrgid(test[1]);
   
   boolean bl1 = tfsjService.updateObject(tfsjk);

 return SUCCESS;
  } catch (Exception ex) {
   return INPUT;
  }
 }

 

 

//创建新session来select

public String[] getRs(String tfsjid) {
  String[] rs = new String[2];
  Session session = HibernateSessionFactory.getSession();
  Tfsjk tf = ((Tfsjk) session.createQuery("from Tfsjk t where t.id=" + tfsjid).iterate().next());
  rs[0] = tf.getSjzt();
  rs[1] = tf.getXfOrgid();
  HibernateSessionFactory.closeSession();
  return rs;
 }

 

你可能感兴趣的:(C++,c,Hibernate,servlet,C#)