Get current object ID in Generic DAO. Hibernate

773 Views Asked by At

I want to delete all objects by their IDs in Hibernate Generic Dao implementation.

For now I created method like this:

  @Override
    public void deleteByIds(Collection<Serializable> ids) {
        getSession()
                .createQuery("delete from " + getDomainClass() + " where id in (:ids)")
                .setParameter("ids", ids)
                .executeUpdate();
    }

but id propery in query is undefined. It's mean I can have this method only on entities using @NamedQueries and it impossible to extract this method to Generic DAO?

1

There are 1 best solutions below

0
Ean V On

Assuming id is available in your hibernate entity, you need to change

setParameter("ids", ids)

to

setParameterList("ids", ids)