Search code examples
hibernategrailsgrails-orm

Execute an Update on an Unmapped Table


I need for some reasons make a delete on a unmapped many-to-many table (automatically generated table). I haven't found out how to execute such a update with in grails or hibernate.

What I would like to have is something like:

session.executeUpdate("delete from person_titles where person_id=?", [personId])

but of course there is not executeUpdate method on a hibernate session instance. And I can't do a session.delete(...), since this table is not mapped.

I hope, there is an easy way to do that.


Solution

  • You should use createSQLQuery method:

    Query deleteQuery = session.createSQLQuery("delete from person_titles where person_id=?");
    deleteQuery.setInteger(0, personId);
    int deleted = deleteQuery.executeUpdate();