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

HQL的update语句
update A 
set a = "12" 
where car = "car1" 
这条sql语句用我想用hibernate执行,该如何操作?我用的是hibernate3.
我试图这样写:
Java code

try {
            session = HibernateSessionFactory.getSession();
            tx = session.beginTransaction();
            String hql = "update Maccount set maccount.weekcount = 12 where maccount.id = 1";
            Query query = session.createSQLQuery(hql).addEntity("maccount",
                    Maccount.class).addJoin("id", "maccount.id").addJoin(
                    "weekcount", "maccount.weekcount");
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            if (tx != null) {
                tx.rollback();
                tx = null;
            }
        } finally {
            if (session != null) {
                session.close();
                session = null;
            }
        }


虽然不报错,但执行出来没有效果。请问各路高僧,这是何原因?

------解决方案--------------------
tx = session.beginTransaction();
String hql = "update Maccount set weekcount = 12 where id = 1";
Query query = session.createQuery(hql);
query.executeUpdate();
tx.commit();