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.

有帮助吗?

解决方案

You should use createSQLQuery method:

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

其他提示

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)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top