Update with Join hibernate (HQL)

喜夏-厌秋 提交于 2019-12-25 02:01:22

问题


I'm on trouble with an hql problem.

I would like to write a query, that updates an attribut, and that's based on a value on another table.

This is my example, I have those two tables : Client and Widhdrawal.

Client : idClient, name ...

Widhdrawal : idWidh, cost, and the idClient (foreign key)

Now if i would update the client, under the condition of (idClient = 5 for example), i can't.

I tried this, but in vain :

        String hql = "UPDATE Widhdrawal W set W.cost = :salary "  + 
    "where W.Client.id_client = :employee_id)";

    Query query = session.createQuery(hql);
    query.setParameter("salary", 1000);
    query.setParameter("employee_id", 5);
    int result = query.executeUpdate();

I hope that someone can have some advices, thank you.


回答1:


Try this:

 String hql = "UPDATE Widhdrawal W set W.cost = :salary "  + 
          "where W.idClient = :employee_id)";



回答2:


Thank you, i found the solution. I hope that this can help other people ... This problem is du to lowerCase.

                String hql = "UPDATE Widhdrawal W set W.cost= :newCost "  + 
                      "where W.client.id_client = :id_cl";

            Query query = session.createQuery(hql);



回答3:


Try this way --

String hql = "UPDATE Widhdrawal W set W.cost = :salary " + "where W.id_client =(select id_client from client where id_client = :employee_id)";

Widhdrawal and client is POJO class name.



来源:https://stackoverflow.com/questions/15514021/update-with-join-hibernate-hql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!