fjloma opened a new issue, #14369:
URL: https://github.com/apache/grails-core/issues/14369

   The list method with max params returns a PagedResultList. This class 
obtains the resulting list by calling  org.hibernate.query.Query.getResultList, 
which does not fire the preQuery event that is neccesary for the TenantResolver 
to be invoked.
   
   This makes the list method to use the last tenant id used in the session, 
instead of resolving the current one, or issuing a query without a tenant.
   
   For example, the next test will fail, because as we clear the tenant id, the 
list method should throw an error.
   ```groovy 
       void "Test list with 'max' parameter"() {
           given: "Create two Authors with tenant T0"
               System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, 
'TENANT')
               MultiTenantAuthor.saveAll([new MultiTenantAuthor(name: "A"), new 
MultiTenantAuthor(name: "B")])
   
           when: "Query with no tenant"
               System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, 
'')
               MultiTenantAuthor.list([max: 2])
           then: "An exception is thrown"
               thrown(TenantNotFoundException)
      }
   ```
   
   If you run this, you can see the query that it is being used:
   ```SQL
       select
           multitenan0_.id as id1_0_,
           multitenan0_.version as version2_0_,
           multitenan0_.tenant_id as tenant_i3_0_,
           multitenan0_.name as name4_0_ 
       from
           multi_tenant_author multitenan0_ limit ?
   ```
   
   However, if we use the list method without any parameters, it works 
correctly and raises the exception.
   
   Another test:
   ```groovy
           given: "Create two Authors with tenant T0"
               System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, 
'TENANT')
               MultiTenantAuthor.saveAll([new MultiTenantAuthor(name: "A"), new 
MultiTenantAuthor(name: "B")])
   
           when: "Query with the same tenant as saved, should obtain 2 entities"
               System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, 
'TENANT')
           then:
               MultiTenantAuthor.list([:]).size() == 2
   
           when: "Query by another tenant, should obtain no entities"
               datastore.sessionFactory.currentSession.clear()
               System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, 
'OTHER TENANT')
               def list = MultiTenantAuthor.list([max: 2])
           then:
               list.size() == 0
   ```
   
   Again, this fails because the list method with max parameter, is using the 
first tenant id 'TENANT' (because it wont fire the preQuery event..). You can 
see that the query used is:
   ```SQL
       select
           multitenan0_.id as id1_0_,
           multitenan0_.version as version2_0_,
           multitenan0_.tenant_id as tenant_i3_0_,
           multitenan0_.name as name4_0_ 
       from
           multi_tenant_author multitenan0_ 
       where
           ? = multitenan0_.tenant_id limit ?
   [Test worker] DEBUG org.hibernate.type.descriptor.sql.BasicBinder - binding 
parameter [1] as [VARCHAR] - [TENANT]
   ```
   
   I've created a fork with this tests and a possible fix here: 
https://github.com/fjloma/gorm-hibernate5
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to