Search code examples
hibernategrailsgroovyhql

HQL Insert Query in Grails


I want to write an insert query in Grails. I have tried all possible combinations but cant get the syntax correct. Can anybody please help?

class Person {
    int age
    String name
}

i tried the following:

Person.executeUpdate("insert into Person  values (20,"ABC")")

p.s.:Please do not mention using save()


Solution

  • Execute a native query:

    def sql = new Sql(sessionFactory.currentSession.connection())
    sql.execute("insert into person values(?,?)", ["foo", "bar"])
    

    note that person is the actual table name.