我有以下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,但我不知道该怎么做,在休眠。
HQL没有比在这种情况下,SQL不同。 只要用逗号分隔列:
String hql = "UPDATE Buchung as b set " +
"STORNO = :Storno," +
"NAME = :Name " +
......
"where ID = :BuchungID";
该语法类似于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);
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();