Repository navigation
Conversation
…(Hibernate 7) In discriminator mode, GORM for Hibernate 7 filters the queries of a multi-tenant class by its tenant id. The filter compared the default column of the tenant id property, so with tenantId name: 'firmId' and firmId column: 'x', every query of the class, of its subclasses, and of a collection of it failed with an unknown column. The filter now takes the column the mapping gives the property, which the binder binds it to, and the default one when the mapping sets none. The Hibernate 7 counterpart of apache#17236. See apache#14365
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A subclass is filtered by a column that is not the one it maps.
| return Optional.ofNullable(getHibernateTenantId()) | ||
| .map(fetcher::getDefaultColumnName) | ||
| .map(defaultColumnName -> ":tenantId = " + defaultColumnName) | ||
| .map(tenantId -> Optional.ofNullable(tenantId.getColumnName(null)) |
There was a problem hiding this comment.
A subclass maps its tenant id to sub_firm. Listing it should use that column. It uses ledger_firm, because the subclass keeps the parent filter. Use the subclass column.
There was a problem hiding this comment.
Thanks. I checked what a subclass's firmId column: 'sub_firm' does, and it creates no sub_firm column. A property a subclass inherits is bound once, on the root, from the root's mapping. That holds with one table, with tablePerHierarchy false, and with tablePerConcreteClass true, where the subclass's own table copies the root's column. So listing the subclass already reads the right column: its rows are filtered by the root's filter on ledger_firm, and that's the column the tenant id is written to.
Where the override did break something is the case @sbglasius points to. The filter of a collection whose elements are of that subclass was built from the subclass's own mapping, and named sub_firm, which doesn't exist. In f6f15f9, the filter condition takes the column of the property the root declares under the tenant id's name, so the collection filter names ledger_firm too. MultiTenancyTenantIdColumnSpec now has a subclass that maps a column of its own in each inheritance mode. Listing it is filtered by the root's column in all three, and a collection of it holds only the current tenant's elements with one table and with a table per class. The collection features fail on 1cfdec1 with Column "SUB_FIRM" not found and Column "UNION_SUB_FIRM" not found.
sbglasius
left a comment
There was a problem hiding this comment.
This is an AI assisted review
The fix looks right for the class, and for a subclass that keeps the parent's tenant column. On James's thread: a subclass doesn't get a filter of its own, and the inherited column is created only on the parent's table, so the parent's filter applies to it. The gap I see is ToManyEntityMultiTenantFilterBinder. It builds the filter from the element's own class, so a subclass that remaps firmId column: would filter its collections on a column that doesn't exist. Could you build the filter from the parent class's tenant id, and add a test with a subclass column override, both with the default single-table setup and with tablePerHierarchy false?
A property a subclass inherits is bound once, on the root, from the root's mapping, in every inheritance mode, so a subclass's own column mapping of an inherited tenant id creates no column. The filter of a collection whose elements are of such a subclass was built from the subclass's mapping and named a column that does not exist. The filter condition now takes the column of the property the root declares under the tenant id's name, and the entity's own tenant id when the root has no such property. See apache#14365
|
Thanks @jamesfredley and @sbglasius. f6f15f9 builds the filter of a subclass from the column its root maps:
All 4,716 tests of |
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A collection load fails when an intermediate class declares the tenant id, and a bracket-quoted column mapping is sent to the database as written.
| // so a tenant id the root declares takes the root's column | ||
| HibernatePersistentProperty tenantId = getHibernateTenantId(); | ||
| HibernatePersistentProperty bound = Optional.ofNullable(tenantId) | ||
| .flatMap(own -> Optional.ofNullable(getRootEntity()) |
There was a problem hiding this comment.
In a Base, Middle, Leaf hierarchy, Middle declares the tenant id and Leaf maps firmId column: 'leaf_firm'. The lookup checks the root, then uses the leaf mapping. Loading a collection of Leaf filters on leaf_firm and fails with column not found. Previously it filtered on the column Middle bound. Use the column that was actually bound.
| .map(root -> ((GrailsHibernatePersistentEntity) root).getHibernatePropertyByName(own.getName()))) | ||
| .orElse(tenantId); | ||
| return Optional.ofNullable(bound) | ||
| .map(property -> Optional.ofNullable(property.getColumnName(null)) |
There was a problem hiding this comment.
On H2, firmId column: '[FIRM_ID]' binds the physical column as a quoted identifier, but the filter emits [FIRM_ID], which H2 rejects. The previous predicate used the bound column name.
Description
The Hibernate 7 counterpart of #17236. In discriminator mode, GORM for Hibernate 7 filters every query of a multi-tenant class by the tenant id. The filter compared the default column of the tenant id property, not the column the mapping gives it. With
the table has a
ledger_firmcolumn, but the filter readsfirm_id, so every query of the class fails withColumn "FIRM_ID" not found. The same goes for a subclass of such a class, and for a collection whose elements are such a class, with or without a join table.GrailsHibernatePersistentEntity.getMultiTenantFilterConditionnow takes the column the mapping gives the tenant id, and the default column only when the mapping sets none. That is the column the binder binds the property to: for a property with no path and no column configuration,ColumnNameForPropertyAndPathFetcherresolves the same mapped column, falling back to the same default. The entity, subclass and collection filters all build their condition through this method. No signature changes.A subclass's own column mapping of an inherited tenant id creates no column: in every inheritance mode, a property a subclass inherits is bound once, on the root, from the root's mapping. So the condition takes the column of the property the root declares under the tenant id's name, and the entity's own tenant id when the root has no such property. Without that, the filter of a collection whose elements are of a subclass that maps a column of its own named a column that does not exist.
Not covered
tablePerHierarchy false). Hibernate applies the collection filter to the subclass's own table, while the tenant id column is in the root's table, so it fails with an unknown column whatever column the filter names, as it does on 8.0.x. Hibernate 7 throws aNullPointerExceptionfor a collection filter with alias maps, so naming the root's table that way does not work. Left for a separate change.Relates to #14365 and #17236.
Tests
MultiTenancyTenantIdColumnSpec(new,grails-data-hibernate7-core, the same spec as in Filter a multi-tenant class by the column its tenant id is mapped to #17236): a query of the class, of a subclass, and of a class with onlytenantId name:returns the current tenant's instances only, and none under a tenant that has none. A collection of such elements, through a foreign key, a criteria join and a join table, holds the current tenant's elements only. On 8.0.x without the change, the 5 cases whose tenant id has a column of its own fail withColumn "FIRM_ID" not found, and the one fortenantId name:alone passes. With the change, all 6 pass.MultiTenancyTenantIdColumnSpecalso has a subclass that maps a tenant id column of its own, with one table, withtablePerHierarchy falseand withtablePerConcreteClass true: listing it is filtered by the root's column in all three, and its collections hold only the current tenant's elements with one table and with a table per class. A collection of a subclass whose root isn't multi-tenant but declares the property it takes as its tenant id is covered too. On 1cfdec1, the 3 collection features fail with an unknown column; the listing features pass there as well.GrailsHibernatePersistentEntitySpec: the unit features ofgetMultiTenantFilterConditioncover a mapping that gives the tenant id a column and one that does not, and a subclass whose root declares the tenant id property and one whose root does not.Verification
grails-data-hibernate7-corepass (20 skipped)../gradlew cleanViolationReports aggregateViolations --continuereports no Checkstyle, CodeNarc, PMD or repository convention violations. SpotBugs is disabled../gradlew :grails-data-hibernate7-core:codeStyleis clean.Generative AI tooling (Claude Code) was used to research the cause, write the change and its tests, and run the checks. I reviewed the change.
Contributor Checklist
Please review the following checklist before submitting your pull request. Pull requests that do not meet these requirements may be closed without review.
Issue and Scope
7.0.x): Bug fixes only. No new features or API changes.7.1.x): New features are welcome, but breaking existing APIs must be avoided.8.0.x): Reserved for major changes. Breaking API changes are permitted.Code Quality
./gradlew build --rerun-tasks. (The tests ofgrails-data-hibernate7-core.)./gradlew codeStyleand resolved any violations. See Code Style for details.Licensing and Attribution
Documentation