hibernate3.0的批量更新

    技术2022-05-11  45

    hibernate3.0的一个重要更新就是增加了批量更新和批量删除的功能。 官方文档给了个例子: Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); String hqlUpdate = "update Customer set name = :newName where name = :oldName"; int updatedEntities = s.createQuery( hqlUpdate ) .setString( "newName", newName ) .setString( "oldName", oldName ) .executeUpdate(); tx.commit(); session.close(); 而在我的一个应用中,需要更新的列刚好是一个表的外键 使用对象的属性来设置where条件总是出错, 而选择使用表的列名来设置where条件却可以了 (更新的列和判断条件的列是同一个)     boolean success = true;     Session session = ThreadLocalSession.getCurrentSession();     try {       ThreadLocalSession.beginTransaction();             StringBuffer hql = new StringBuffer("update com.xxx.MyClass set fkProperty=:newValue where fk_id");       if(pOld == null) {         hql.append(" is null");       } else {         hql.append("=:oldValueId");       }             Query query = session.createQuery(hql.toString());       query.setParameter("newValue", pNew);            if(pOld != null) {         query.setParameter("oldValueId", pOld.getId());       }             query.executeUpdate();     } catch(Exception e) {       success = false;       e.printStackTrace();     } finally {       ThreadLocalSession.endTransaction(success);     } 不知道是我表的设置问题,还是hibernate3.0对批量更新的支持问题

    最新回复(0)