Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update several Columns in one Hibernate Query?

i have the Following HQL:

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

Is it possible to Update more then one column in a HQL? For Example:

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

I know how to do that in MSSQL but i dont know how to do that in Hibernate.

like image 968
Paks Avatar asked Sep 06 '12 09:09

Paks


3 Answers

HQL is no different than SQL in this case. Just use comma to separate columns:

String hql = "UPDATE Buchung as b set " +
          "STORNO = :Storno," +
          "NAME = :Name " +
           ......  
          "where ID = :BuchungID";
like image 181
Miroslav Popovic Avatar answered Nov 13 '22 07:11

Miroslav Popovic


The syntax is similar to the SQL syntax, but with mapped fields/properties instead of columns:

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

Note that if the goal is to modify a single entity instance, you'd better do

Buchung b = (Buchung) session.get(Buchung.class, buchungId);
b.setStorNo(newStorno);
b.setName(newName);
like image 45
JB Nizet Avatar answered Nov 13 '22 07:11

JB Nizet


    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();

in normal, you must have "transaction" to run query

    Transaction transaction = null;
transaction = session.begintransaction();
...
transaction.commit();
like image 25
dungth4 Avatar answered Nov 13 '22 08:11

dungth4