Update several Columns in one Hibernate Query?

前端 未结 3 2300
渐次进展
渐次进展 2021-02-19 08:01

i have the Following HQL:

String hql = \"UPDATE Buchung as b \" +
             \"set STORNO = :Storno \" +
             \"where ID = :BuchungID\";
相关标签:
3条回答
  • 2021-02-19 08:20
        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();
    
    0 讨论(0)
  • 2021-02-19 08:24

    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";
    
    0 讨论(0)
  • 2021-02-19 08:40

    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);
    
    0 讨论(0)
提交回复
热议问题