使用Hibernate更新语句

时间:2014-04-02 20:22:22

标签: hibernate

任何人都可以帮我在Hibernate中实现以下逻辑吗?

UPDATE CUSTOMER 
SET balance  = (SELECT balance  FROM CUSTOMER WHERE Name='John')
WHERE NAME='Adam' 

提前致谢

1 个答案:

答案 0 :(得分:0)

最简单的方法是直接进行SQL查询:

session.createSQLQuery("UPDATE CUSTOMER"
                   + "  SET balance  = ("
                   + "     SELECT balance"
                   + "     FROM CUSTOMER"
                   + "     WHERE Name='John')"
                   + "  WHERE NAME='Adam'")
       .executeQuery();

Safier将使用对象模型。

Customer adam = session.createQuery("from Customer where name = :name").setParameter("name", "Adam").uniqueResult();
Customer john = session.createQuery("from Customer where name = :name").setParameter("name", "John").uniqueResult();
adam.setBalance(john.getBalance);
transaction.commit;