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.

Était-ce utile?

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

Autres conseils

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)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top