xrcwrn
xrcwrn

Reputation: 5327

Hibernate updation not working

I am using following method to update data in database.

     String hql = "UPDATE EmployeeSalary set salary = :sl,"
                        + "monthYr=:dt "
                        + "WHERE id =:id and client.id=:cid";
            for (EmployeeSalary e : eList) {
                Query query = session.createQuery(hql);

                query.setParameter("sl", e.getSalary());
                query.setParameter("dt", e.getMonthYr());
                query.setParameter("id", e.getId());
                query.setParameter("cid", e.getClient().getId());

                int result = query.executeUpdate();
                System.out.println("result is " + result);
                if (eAttList.size() % 20 == 0) {
                    session.flush();
                    session.clear();
                }
            }

Is there any problem with code.

On execution it is showing

result is 0

How to resolve above problem.

Upvotes: 0

Views: 59

Answers (1)

JB Nizet
JB Nizet

Reputation: 691715

The documentation about update queries says:

No "Forms of join syntax", either implicit or explicit, can be specified in a bulk HQL query. Sub-queries can be used in the where-clause, where the subqueries themselves may contain joins.

Your query seems to violate this rule: client.id=:cid is an implicit join to the client entity.

Note that you're making your life difficult. You could simply get the entity by ID from the session (using Session.get()), and update it. Update queries are useful to update many rows at once.

Upvotes: 1

Related Questions