WaZ
WaZ

Reputation: 1757

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

Upvotes: 2

Views: 8097

Answers (2)

armandino
armandino

Reputation: 18528

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.

Upvotes: 6

Burt Beckwith
Burt Beckwith

Reputation: 75671

It doesn't look possible. See http://docs.jboss.org/hibernate/core/3.3/reference/en/html/batch.html#batch-direct and note that it says "Only the INSERT INTO ... SELECT ... form is supported; not the INSERT INTO ... VALUES ... form." So you can insert as a select from one or more other tables, but can't insert directly like you would with save().

Upvotes: 4

Related Questions