Repository navigation
fix: fail fast when a SCHEMA multi-tenant entity references a non-tenant entity - #16850
borinquenkid wants to merge 8 commits into
Conversation
…that are not multi-tenant Each tenant schema gets a session factory that maps the MultiTenant entities only, so a hasMany or to-one association from a MultiTenant entity to any other entity failed at startup with 'An association ... refers to an unmapped class'. Startup now fails with a ConfigurationException that names the associations and the ways out, in both bindings. The schema-per-tenant guide describes the restriction. Fixes #14345 Claude-Session: https://claude.ai/code/session_01Dmki4SQGwRSj8pRJ5KCWyX
jamesfredley
left a comment
There was a problem hiding this comment.
Not approving. A non-tenant target that is mapped on every connection is rejected at startup.
| if (!(property instanceof Association) || property instanceof Embedded || property instanceof EmbeddedCollection) { | ||
| continue; | ||
| } | ||
| PersistentEntity target = ((Association<?>) property).getAssociatedEntity(); |
There was a problem hiding this comment.
A tenant owner has a many-to-one to a non-tenant entity that is mapped on every connection. Startup throws. That target is present. Do not reject it.
There was a problem hiding this comment.
I could not reproduce this one. In 18bb5d5 I added SchemaTenantAssociationLayoutSpec (Hibernate 5 and 7) with the target layouts a mapping can declare: no datasource is refused; datasource 'ALL', datasources(['ALL']), a subclass of a root that uses ALL, the root of that hierarchy, and ALL given through grails.gorm.default.mapping all start. Which declaration of the target still throws for you? A mapping snippet is enough, and I will add it as a row.
… refuses and accepts A tenant owner pointing at a target with no datasource declared is refused; one pointing at a target that declares ALL (alone or as a list), a subclass or the root of a hierarchy that uses every connection, or a target covered by the default mapping starts. On Hibernate 5 and 7. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
| continue; | ||
| } | ||
| PersistentEntity target = ((Association<?>) property).getAssociatedEntity(); | ||
| if (target != null && !target.isMultiTenant() && !getConnectionSourceNames(target).contains(ConnectionSource.ALL)) { |
There was a problem hiding this comment.
The resolver returns only tenant1. Owner is multi-tenant and has Shared target. Shared is not multi-tenant and lists default and tenant1. Startup should succeed. This reports Owner.target -> Shared and throws, because ALL is absent. Check whether the target is actually mapped on the tenant connection.
…ses ALL Answers the review of #16850: a non-tenant target that lists the default connection and the tenant schemas was refused for lacking ALL. The check now runs against the tenant schemas known at startup, and a target has to be mapped on every connection that maps the owner. The guide says so. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
Answers the second review. A non-tenant target is now checked against the tenant schemas known at startup, and against the default connection, because a
Two limits, both written in the guide or the spec: a tenant schema added after startup is not checked, and a target that lists only the tenant schema is refused by Hibernate with a |
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A target mapped to every named tenant schema is accepted. A target mapped by annotation, with no datasource list, is still rejected even though it is mapped on every schema.
| Set<String> names = new LinkedHashSet<>(tenantSchemaNames); | ||
| names.add(ConnectionSource.DEFAULT); | ||
| for (String name : names) { | ||
| if (usesConnectionSource(owner, name) && !usesConnectionSource(target, name)) { |
There was a problem hiding this comment.
The owner is multi-tenant. The target is mapped by annotation and has no datasource list. Startup used to succeed, because that target is mapped on every schema. This treats it as the default schema only and throws. Check the mapping that is actually used.
…hema The startup check of schema multi-tenancy refused an association from a multi tenant entity to a target that lists no datasource, whatever mapped the target. The Hibernate binders add every class annotated with jakarta.persistence.Entity to every session factory and do not read its datasource list, so such a target is mapped on each tenant schema and the association has something to refer to; startup used to succeed and the check refused it. The check now treats a JPA-annotated target as mapped everywhere, and asks the datasource lists only of the others. SchemaTenantAssociationLayoutSpec, on Hibernate 5 and Hibernate 7, gains the layout with the tenant schemas unknown and known. A probe of the session factory of a tenant schema shows the JPA target mapped there on both, and a GORM-annotated target with no datasource list not mapped. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
Thank you, you are right, and I checked what the datastore does before changing the check. I added a probe that starts a datastore in SCHEMA mode, adds a tenant schema and lists the entities its session factory maps:
The check read only the datasource list, so it refused the first layout although the association has something to refer to. It now treats a JPA-annotated target as mapped on every schema, and asks the datasource lists only of the others.
Branch policy: The mapping already failed at startup with an unclear unmapped-class error; this makes the same failure name the association, and accepts a target that is mapped on every connection of the owner, whether by datasource ALL or by a JPA annotation. |
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A target mapped by annotation is accepted. A property stored as a value, not a relationship, is still rejected.
| continue; | ||
| } | ||
| for (PersistentProperty<?> property : entity.getPersistentProperties()) { | ||
| if (!(property instanceof Association) || property instanceof Embedded || property instanceof EmbeddedCollection) { |
There was a problem hiding this comment.
The owner maps items as a serializable value, not as a relationship. Startup used to succeed. This treats items as an association that is missing from the tenant schema and throws. Skip a property that is stored as a value.
|
The Hibernate 5 half is now #17056 against 7.0.x: the startup failure only gets a clearer message, so it goes to the oldest branch with the problem (dev@ rule). This PR keeps the Hibernate 7 half and the two known-tenant layouts; once the 7.0.x PR is merged and merged up, the Hibernate 5 files here drop out of the diff. |
…HibernateDatastore The finder was a public static method of ConnectionSourcesSupport, new API on a patch line. It is now private code in HibernateDatastore, as in the Hibernate 5 fix on 7.0.x; its spec of the shared class goes with it and the Hibernate 7 specs cover the check through the datastore. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
…HibernateDatastore Same change as the Hibernate 5 fix on 7.0.x, so the two merge without a conflict. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
Pre-push review of this PR and its 7.0.x half #17056: the check was a new public static method of |
jdaugherty
left a comment
There was a problem hiding this comment.
The checker now accounts for ALL, named connections, and JPA-annotated targets, and moving it into private datastore methods removes the added shared public API. Two concrete issues remain in the current diff.
P2 — Exclude Hibernate 5 properties actually bound as basic values. HibernateDatastore.java:787–793 still checks every non-embedded association. With a known tenant1, a MultiTenant owner can declare Set<Target> items, hasMany = [items: Target], and items type: 'serializable', where Target is serializable and default-only. The Hibernate 5 binder:2032–2037 stores that collection in a basic column, so it needs no tenant target mapping. The new checker nevertheless rejects startup. Please classify the effective binding before checking target membership and add a public startup regression. This remains the basic-value concern from the earlier review.
P2 — Use DEFAULT in the named-connection fixtures and remedy. The Hibernate 7 fixture:260–265, its Hibernate 5 counterpart, and both schema guides use lowercase default. Matching is case-sensitive; that leaves the target out of the default factory, so the supposed startup-success case fails before this check. Change it to datasources(['DEFAULT', 'tenant1']) and verify both the successful and missing-tenant cases.
These findings are source-based; I have not rerun the tests.
… that the binder maps as a basic value A collection that a serializable type maps, by its own mapping or by the mapping of its class, is stored in a basic column, so its target needs no mapping in a tenant schema. The check refused it at startup. It now classifies the effective binding before it looks at the target, and a collection that is a relationship is still refused. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
1. Properties bound as basic values. Fixed in fb30a4c. The check looks at the effective binding before it looks at the target: a collection that a serializable type maps, by the property's own mapping ( 2.
So the fixtures and the guides use the right spelling here. This may be the other way round on 7.0.x, where the default name is the uppercase one; the same row on #17276 should use whatever that branch's constant is. If you have a case on this head where |
Fixes #14345
With
grails.gorm.multiTenancy.mode: SCHEMA, each tenant schema gets its own SessionFactory, and only MultiTenant entities (plus entities that declare theALLdatasource) are mapped into it. An association from a MultiTenant entity to any other entity therefore failed at startup with a hard-to-diagnose "refers to an unmapped class" error.This PR does not add support for such associations. It makes the failure clear:
HibernateDatastore(Hibernate 5 and 7) now throws aConfigurationExceptionat startup in SCHEMA mode that names each offendingOwner.property -> Targetand says to make the target MultiTenant or use DISCRIMINATOR mode.ConnectionSourcesSupport.findAssociationsToEntitiesMissingFromTenantSchemasfinds the offending associations (embedded types and targets declaringALLare skipped).ConnectionSourcesSupportSpec, plusSchemaTenantAssociationSpecin both Hibernate cores.schemaPerTenant.adocfiles.https://claude.ai/code/session_01Dmki4SQGwRSj8pRJ5KCWyX
Branch policy: the startup check is this pull request's own new refusal of an association from a multi tenant entity to an entity that a tenant schema does not map; a property that the binder maps as a basic value is not refused.