Grails Foreign Key Update - Performance Update Object

Refresh

December 2018

Views

94 time

2

I am creating a lot of objects and I am trying to get a bit of a performance boost. Currently I have an assignment for a gorm object attribute that I could get better performance out of if I could set the foreign key manually.

For Example

def oldBook = Book.findByTitle("Of Mice and Men")
def newBook = new Book()
newBook.author = oldBook.author //This line could be faster

The line that could be faster if it didn't have to look up the author object on the oldBook. Ideally the line would look more like this. (fyi these don't actually work)

newBook.authorId = oldBook.authorId
// or
newBook.setAuthorId(oldBook.authorId)

Is something like this possible in grails? Perhaps it's possible but dangerous. I'm using version grails 2.2.1, Hibernate, and Oracle. The only other thought I had on this is I could eagerly load the author object.

1 answers

3

Это ваш лучший выбор:

newBook.author = Author.load(oldBook.authorId)

Это имеет преимущество динамического fooIdсвойства , которое не влечет за собой загрузку книги , Authorнапример. Используя load()метод создает прокси для Authorс только idзначением , хранящимся, и до тех пор , как только idдоступ, не будет база данных попала полностью загрузить экземпляр. Это в основном делает прокси - обертку для значения FK.