You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We found that when we do the query the books collection of the author is not initialized. So it is not eager loaded. It raises a nonLazyInitialization exception when we access to the collection out of the session.
The problem is that when we make the query, the collection is marked as lazy = true and the persistence context does not initializes it.
We did a search to find the exact location when lazy passes from false to true, and it is in the class GrailsDomainBinder, in the method bindCollectionForPropertyConfig
In this case the flights association will be loaded at the same time as its Airport instance, although a second query will be executed to fetch the collection. You can also use fetch: 'join' instead of lazy: false.....So, the recommendation is currently to use fetch: 'join' for single-ended associations and lazy: false for one-to-manys.
So I understand that it should work with lazy = false and FetchMode not JOIN (join can retrieve duplicated results, that is the reason we are not using it)
So, we find that the documentation is not coherent with the code, and we do not know if this is a bug or there is a reason to be like that.
Thanks.
The text was updated successfully, but these errors were encountered:
abelenguer
changed the title
GrailsDomainBinder set lazy false when FetchMode is not JOIN
GrailsDomainBinder set lazy true in coolections when FetchMode is not JOIN
Oct 31, 2019
abelenguer
changed the title
GrailsDomainBinder set lazy true in coolections when FetchMode is not JOIN
GrailsDomainBinder set lazy true in collections when FetchMode is not JOIN
Oct 31, 2019
* Upgrade to Liquibase 3.10.1 (also liquibase-hibernate5)
- Fixed tests by extracting changeLog part (Gradle verbose log causes polluted outputCapture)
- Fixes GroovyChangeLogSpec if default JVM langauge is not en.
- String comparisons are now done without spaces in tests
- fixes#95#100 #159
* Use openjdk8 in Travis
'org.springframework.boot' version '2.1.3.RELEASE'
org.grails:grails-datastore-gorm-hibernate5:7.0.0.RC1
In this application:
https://github.com/abelenguer/grails3-gorm
we have an author entity:
`class Author implements GormEntity {
with a collection of books, with a non lazy mapping.
`@ToString(includes = "title")
@entity
class Book implements GormEntity {
}
`
When we run it and acces to the index operation of the controller from url (http:localhost:8080/index)
String index() { Long authorId = bookService.saveAuthor() bookService.findById(authorId)
it creates an author and find the books by authorId.
` String findById(Long id) {
Author author = Author.findById(id)
logger.info("Is initialized: " + GrailsHibernateUtil.isInitialized(author, "books"))
We found that when we do the query the books collection of the author is not initialized. So it is not eager loaded. It raises a nonLazyInitialization exception when we access to the collection out of the session.
The problem is that when we make the query, the collection is marked as lazy = true and the persistence context does not initializes it.
We did a search to find the exact location when lazy passes from false to true, and it is in the class GrailsDomainBinder, in the method bindCollectionForPropertyConfig
FetchMode fetch = config.getFetchMode(); if (!fetch.equals(FetchMode.JOIN) && !fetch.equals(FetchMode.EAGER)) { collection.setLazy(true); }
This condition makes that if the fetch is not JOIN, the lazy changes from false to true. We checked that with fetch join is working, but that is not what we want. In the documentation (http://gorm.grails.org/7.0.0.RC1/hibernate/manual/index.html#fetching) is:
So I understand that it should work with lazy = false and FetchMode not JOIN (join can retrieve duplicated results, that is the reason we are not using it)
So, we find that the documentation is not coherent with the code, and we do not know if this is a bug or there is a reason to be like that.
Thanks.
The text was updated successfully, but these errors were encountered: