Skip to content

GrailsDomainBinder set lazy true in collections when FetchMode is not JOIN #14446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
abelenguer opened this issue Oct 31, 2019 · 0 comments
Open

Comments

@abelenguer
Copy link

abelenguer commented Oct 31, 2019

'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 {

Set<Book> books
String name

static hasMany = [
        books : Book
]

static mapping = {
    books lazy: false
}`

with a collection of books, with a non lazy mapping.

`@ToString(includes = "title")
@entity
class Book implements GormEntity {

String title
Author author

static belongsTo = [author: Author]

static mapping = {
    author lazy: false
}

}
`
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"))

    author.discard()

    author.books.join(",")
}`

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:

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.

@abelenguer 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 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
jamesfredley referenced this issue in grails/grails-data-hibernate5 Feb 19, 2025
* 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
@jdaugherty jdaugherty transferred this issue from grails/grails-data-hibernate5 Mar 13, 2025
@jdaugherty jdaugherty transferred this issue from apache/grails-data-mapping Apr 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants