Question

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.

Was it helpful?

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();

OTHER TIPS

I've found a way to achieve that myself:

def sql = new groovy.sql.Sql (dataSource)
def deleteCommand = "delete from person_titles where person_id = ${personId}"
def result = sql.execute(deleteCommand)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top