15

我有以下 HQL:

String hql = "UPDATE Buchung as b " +
             "set STORNO = :Storno " +
             "where ID = :BuchungID";

是否可以在 HQL 中更新多于一列?例如:

String hql = "UPDATE Buchung as b " +
              "set STORNO = :Storno " +
              "set NAME = :Name " +
               ......  
              "where ID = :BuchungID";

我知道如何在 MSSQL 中做到这一点,但我不知道如何在 Hibernate 中做到这一点。

4

3 回答 3

31

在这种情况下,HQL 与 SQL 没有什么不同。只需使用逗号分隔列:

String hql = "UPDATE Buchung as b set " +
          "STORNO = :Storno," +
          "NAME = :Name " +
           ......  
          "where ID = :BuchungID";
于 2012-09-06T09:02:51.660 回答
3

语法类似于 SQL 语法,但使用映射字段/属性而不是列:

update Buchung set storNo = :storno, name = :name where id = :buchungID

请注意,如果目标是修改单个实体实例,您最好这样做

Buchung b = (Buchung) session.get(Buchung.class, buchungId);
b.setStorNo(newStorno);
b.setName(newName);
于 2012-09-06T09:03:39.307 回答
1
    String hql = "UPDATE Buchung as b set " +
          "STORNO = :Storno," +
          "NAME = :Name " +
           ......  
          "where ID = :BuchungID";

Query qr = session.createSQLQuery(hql);

qr.setParameter("Storno","sto_value");

qr.setParameter("Name","name_value");

...

qr.executeUpdate();

正常情况下,您必须有“事务”才能运行查询

    Transaction transaction = null;
transaction = session.begintransaction();
...
transaction.commit();
于 2015-08-05T07:26:48.693 回答