grails / grails-core

The Grails Web Application Framework
http://grails.org
Apache License 2.0
2.78k stars 951 forks source link

Grails GORM Multi Tenant Lazy Load Not working #11393

Open hmtmcse opened 5 years ago

hmtmcse commented 5 years ago

Recently I upgrade Our Old Custom multi tenant system to Grails 4 Multi Tenant, here seeing Lazy Loading not working

Domain

  1. Page
    1. many to many with MetaTags

Service

@CurrentTenant
PageService
    public Page getSitePage(String _url) {
        return Page.where {
            url == _url
            isInTrash == false
            isActive == true
            visibility != DomainConstants.PAGE_VISIBILITY.HIDDEN
        }.get();
    [}]

When i tried to access the metaTags from GSP it's giving Error

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.xyz.Page.metaTags, could not initialize proxy - no Session
    at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:602)
    at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:217)
    at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:581)
    at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:148)
    at org.hibernate.collection.internal.PersistentBag.iterator(PersistentBag.java:303)
    at gsp_core_layoutssitepage_gsp$_run_closure1.doCall(gsp_core_layoutssitepage_gsp.groovy:69)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

alt text

davydotcom commented 4 years ago

I noticed Hibernate had an update related to Tenant loading. Wondering if that is related?

puneetbehl commented 4 years ago

It looks like you are trying to initialize the hibernate proxy from the view where the session is already closed. I would recommend to not use domain class in the views and instead create a separate DTO class to pass to the model.