我有以下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中做到这一点。
发布于 2012-09-06 17:02:52
在这种情况下,HQL与SQL没有什么不同。只需使用逗号分隔列:
String hql = "UPDATE Buchung as b set " +
"STORNO = :Storno," +
"NAME = :Name " +
......
"where ID = :BuchungID";发布于 2012-09-06 17:03:39
语法类似于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);发布于 2015-08-05 15:26:49
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 transaction = null;
transaction = session.begintransaction();
...
transaction.commit();https://stackoverflow.com/questions/12296499
复制相似问题