Xilang
Xilang

Reputation: 101

grails call hibernate update

Have another question related to Grails issue with unique/save/update.

is there any way to avoid call def bookInstance.get(bookList.id[it]) ? for example, I still want to overwrite the exist data in DB, but I don't want to load exist one from DB, So i call def bookInstance = new Book() bookInstance.id=XXX bookInstance.save()

This does not work, because gorm take it as a new Object, is there any way to force update exist one? like call hibernateSession.update

Upvotes: 0

Views: 434

Answers (1)

Burt Beckwith
Burt Beckwith

Reputation: 75671

Loading the instance shouldn't be very expensive, so you're probably prematurely optimizing. But you can use executeUpdate to update data without retrieving it first. For example if your Book class looks like this:

class Book {
    String author
    String title
}

then you can update it like this:

Book.executeUpdate(
    'update Book b set b.author=:author, b.title=:title where b.id=:id',
    [author: params.author, title: params.author, id: params.id])

Upvotes: 1

Related Questions