This is an automated email from the ASF dual-hosted git repository. borinquenkid pushed a commit to branch 9.0.x-h7-improvement-phase3 in repository https://gitbox.apache.org/repos/asf/grails-core.git
commit eb82a95c44c9680e01bca21f096a6bc85ce246b8 Author: Walter Duque de Estrada <[email protected]> AuthorDate: Fri Oct 9 10:11:31 2026 -0500 fix(hibernate7): accept the tenant id as a part of a composite identifier in native binding grails-test-examples/hibernate7/issue450 maps `id composite: ['id', 'tenantId']` on a multi-tenant entity. The classic binder binds the tenant id like any other column of the key and the tenant filter compares that column; the generator refused it as "a HibernateTenantIdProperty, which the generator does not support yet", so the app did not start once native binding became the default. The generator now treats a tenant id part like a simple one. GeneratedDomainClassesMultiTenancySpec boots the mapping in both modes, stores a row per tenant and reads each tenant's own rows back (it failed natively with the refusal before the fix); the frozen classic oracle gains the records of the new fixture (REFREEZE, additions only). Three specs relied on the old default to boot the classic binder and now state it: the classic boot of the generator differential (with the create-drop the no-argument constructor applied, which resolves the SQL types the records hold), and the two defect-pinning specs that build their own datastore, one of which therefore leaves classic-only-specs.txt. Co-Authored-By: Claude Fable 5.1 <[email protected]> --- grails-data-hibernate7/core/classic-only-specs.txt | 3 +- .../domainbinding/jpa/GrailsDomainGenerator.groovy | 4 +- .../GeneratedDomainClassesMultiTenancySpec.groovy | 58 ++++++++++++++++++++++ .../GrailsDomainBinderCompositeIdDefectSpec.groovy | 11 ++-- ...DomainBinderEmbeddedCollectionDefectSpec.groovy | 5 +- .../GrailsDomainGeneratorDifferentialSpec.groovy | 6 ++- .../resources/classic-oracle/ddl-differential.txt | 4 ++ .../classic-oracle/generator-differential.txt | 3 ++ 8 files changed, 84 insertions(+), 10 deletions(-) diff --git a/grails-data-hibernate7/core/classic-only-specs.txt b/grails-data-hibernate7/core/classic-only-specs.txt index f2b041d674..8fd78eb2f7 100644 --- a/grails-data-hibernate7/core/classic-only-specs.txt +++ b/grails-data-hibernate7/core/classic-only-specs.txt @@ -28,13 +28,12 @@ # InFlightMetadataCollector or MetadataBuildingContext. Only those that FAIL with a natively bound datastore are listed, # one fully qualified class name per line; the others run and pass in both modes (every other binder-internals spec, # the generator differential included, was run against a natively bound datastore before being left out of this list). -# The six listed assert facts of the classic boot model (a collection table the classic binder creates, a unique key it +# The five listed assert facts of the classic boot model (a collection table the classic binder creates, a unique key it # drops, the type parameters it sets) or pin classic defects with @PendingFeature, which native binding does not have, # so they only hold against a classic datastore. The list goes away with the classic binder. # org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateBasicPropertySpec org.grails.orm.hibernate.cfg.domainbinding.jpa.EntityCollectionKeyDefectSpec -org.grails.orm.hibernate.cfg.domainbinding.jpa.GrailsDomainBinderEmbeddedCollectionDefectSpec org.grails.orm.hibernate.cfg.domainbinding.jpa.GrailsDomainBinderOptionDefectSpec org.grails.orm.hibernate.cfg.domainbinding.jpa.GrailsDomainGeneratorSpec org.grails.orm.hibernate.cfg.domainbinding.jpa.GrailsDomainGeneratorUniqueGroupSpec diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/jpa/GrailsDomainGenerator.groovy b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/jpa/GrailsDomainGenerator.groovy index 59ba39334a..9a20f2b486 100644 --- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/jpa/GrailsDomainGenerator.groovy +++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/jpa/GrailsDomainGenerator.groovy @@ -615,7 +615,9 @@ class GrailsDomainGenerator { 'does not support because Hibernate cannot state one in an identifier class. Map the properties of the ' + 'embedded class as plain properties of the identifier instead (the schema stays the same), see the ' + 'Native Domain Binding chapter of the manual' - } else if (!(part instanceof HibernateSimpleProperty) || isDerived(part)) { + } else if (!(part instanceof HibernateSimpleProperty || part instanceof HibernateTenantIdProperty) || isDerived(part)) { + // the tenant id of a multi-tenant entity is bound like any other column of the key, and the tenant filter + // compares that column (CompositeIdBinder gives it no special treatment either) return "Composite identifier part [${part.name}] of [${entity.name}] is a ${part.getClass().simpleName}, which the " + 'generator does not support yet' } else if (!supports(part)) { diff --git a/grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/generated/GeneratedDomainClassesMultiTenancySpec.groovy b/grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/generated/GeneratedDomainClassesMultiTenancySpec.groovy index dc658a7470..b17e877d6b 100644 --- a/grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/generated/GeneratedDomainClassesMultiTenancySpec.groovy +++ b/grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/generated/GeneratedDomainClassesMultiTenancySpec.groovy @@ -23,6 +23,7 @@ import grails.gorm.annotation.Entity import org.hibernate.dialect.H2Dialect import spock.lang.AutoCleanup import spock.lang.Specification +import spock.lang.Unroll import spock.util.environment.RestoreSystemProperties import org.grails.datastore.mapping.core.DatastoreUtils @@ -79,6 +80,52 @@ class GeneratedDomainClassesMultiTenancySpec extends Specification { GdcTenantItem.withNewSession { GdcTenantItem.list()*.name } == ['mustang'] GdcTenantItem.withNewSession { GdcTenantItem.findByName('model s') } == null } + + /** + * The tenant id may be a part of a composite identifier (grails-test-examples/hibernate7/issue450): the classic binder binds + * the part like any other column of the key, and the tenant filter compares the same column. + */ + @Unroll + def "the tenant id can be a part of the composite identifier (generated: #generated)"() { + given: + System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, 'grails') + datastore = new HibernateDatastore(DatastoreUtils.createPropertyResolver([ + 'grails.gorm.multiTenancy.mode' : MultiTenancySettings.MultiTenancyMode.DISCRIMINATOR, + 'grails.gorm.multiTenancy.tenantResolverClass': SystemPropertyTenantResolver.name, + 'dataSource.url' : "jdbc:h2:mem:gdcTenantComposite${generated};LOCK_TIMEOUT=10000".toString(), + 'dataSource.dialect' : H2Dialect.name, + 'hibernate.cache.queries' : 'false', + 'hibernate.cache.use_query_cache' : 'false', + 'hibernate.hbm2ddl.auto' : 'create', + 'hibernate.generatedDomainClasses' : generated, + ]), GdcTenantCompositeBook) + String grailsId = UUID.randomUUID().toString() + String groovyId = UUID.randomUUID().toString() + + when: + GdcTenantCompositeBook.withTransaction { + new GdcTenantCompositeBook(id: grailsId, title: 'The definitive Guide to Grails 2').save(flush: true, failOnError: true) + } + System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, 'groovy') + GdcTenantCompositeBook.withTransaction { + new GdcTenantCompositeBook(id: groovyId, title: 'Groovy in Action').save(flush: true, failOnError: true) + } + + then: 'the primary key spans both parts and the tenant id is stored with the row' + datastore.sessionFactory.mappingMetamodel.getEntityDescriptor(GdcTenantCompositeBook).identifierMapping.jdbcTypeCount == 2 + GdcTenantCompositeBook.withNewSession { GdcTenantCompositeBook.list()*.title } == ['Groovy in Action'] + GdcTenantCompositeBook.withNewSession { GdcTenantCompositeBook.list().collect { [it.id, it.tenantId] } } == [[groovyId, 'groovy']] + + when: + System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, 'grails') + + then: + GdcTenantCompositeBook.withNewSession { GdcTenantCompositeBook.list()*.title } == ['The definitive Guide to Grails 2'] + GdcTenantCompositeBook.withNewSession { GdcTenantCompositeBook.findByTitle('Groovy in Action') } == null + + where: + generated << [false, true] + } } @Entity @@ -86,3 +133,14 @@ class GdcTenantItem implements MultiTenant<GdcTenantItem> { String name String tenantId } + +@Entity +class GdcTenantCompositeBook implements MultiTenant<GdcTenantCompositeBook>, Serializable { + String id + String tenantId + String title + + static mapping = { + id composite: ['id', 'tenantId'] + } +} diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/jpa/GrailsDomainBinderCompositeIdDefectSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/jpa/GrailsDomainBinderCompositeIdDefectSpec.groovy index 259cb100e0..d391992d3f 100644 --- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/jpa/GrailsDomainBinderCompositeIdDefectSpec.groovy +++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/jpa/GrailsDomainBinderCompositeIdDefectSpec.groovy @@ -43,10 +43,13 @@ import spock.lang.PendingFeature */ class GrailsDomainBinderCompositeIdDefectSpec extends HibernateGormDatastoreSpec { + /** The defects are the classic binder's, so the datastores select it whatever the default is. */ + private static final Map<String, Object> CLASSIC = ['hibernate.generatedDomainClasses': false] + @PendingFeature(reason = 'the key of a join table to a composite identifier gets the columns of the element, so Hibernate refuses the foreign key') void "a unidirectional collection of entities with a composite identifier can be bound"() { when: - HibernateDatastore datastore = new HibernateDatastore(UnbootableJoinToComposite, UnbootableComposite) + HibernateDatastore datastore = new HibernateDatastore(CLASSIC, UnbootableJoinToComposite, UnbootableComposite) then: notThrown(Exception) @@ -58,7 +61,7 @@ class GrailsDomainBinderCompositeIdDefectSpec extends HibernateGormDatastoreSpec @PendingFeature(reason = 'ForeignKeyColumnCountCalculator counts a composite identifier part that refers to an entity with a simple identifier as no column, so the foreign key has fewer columns than the key and Hibernate refuses it') void "a foreign key to a composite identifier with a part that refers to an entity can be bound"() { when: - HibernateDatastore datastore = new HibernateDatastore(UnbootableRefToParts, UnbootableParts, UnbootableTarget) + HibernateDatastore datastore = new HibernateDatastore(CLASSIC, UnbootableRefToParts, UnbootableParts, UnbootableTarget) then: notThrown(Exception) @@ -70,7 +73,7 @@ class GrailsDomainBinderCompositeIdDefectSpec extends HibernateGormDatastoreSpec @PendingFeature(reason = 'CompositeIdentifierToManyToOneBinder expands a nested composite identifier one level only, so a foreign key to a composite identifier three levels deep has fewer columns than the key') void "a foreign key to a composite identifier three levels deep can be bound"() { when: - HibernateDatastore datastore = new HibernateDatastore(UnbootableRefToTop, UnbootableTop, UnbootableMiddle, UnbootableFlat) + HibernateDatastore datastore = new HibernateDatastore(CLASSIC, UnbootableRefToTop, UnbootableTop, UnbootableMiddle, UnbootableFlat) then: notThrown(Exception) @@ -82,7 +85,7 @@ class GrailsDomainBinderCompositeIdDefectSpec extends HibernateGormDatastoreSpec @PendingFeature(reason = 'the key of a joined subclass is bound with one column, so a joined subclass of an entity with a composite identifier cannot be bound') void "a joined subclass of an entity with a composite identifier can be bound"() { when: - HibernateDatastore datastore = new HibernateDatastore(UnbootableJoinedParent, UnbootableJoinedChild) + HibernateDatastore datastore = new HibernateDatastore(CLASSIC, UnbootableJoinedParent, UnbootableJoinedChild) then: notThrown(Exception) diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/jpa/GrailsDomainBinderEmbeddedCollectionDefectSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/jpa/GrailsDomainBinderEmbeddedCollectionDefectSpec.groovy index 8ccdc5c09a..b8be64ee3d 100644 --- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/jpa/GrailsDomainBinderEmbeddedCollectionDefectSpec.groovy +++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/jpa/GrailsDomainBinderEmbeddedCollectionDefectSpec.groovy @@ -33,10 +33,13 @@ import spock.lang.PendingFeature */ class GrailsDomainBinderEmbeddedCollectionDefectSpec extends HibernateGormDatastoreSpec { + /** The defects are the classic binder's, so the datastores select it whatever the default is. */ + private static final Map<String, Object> CLASSIC = ['hibernate.generatedDomainClasses': false] + @PendingFeature(reason = 'the role, the table and the key column of a collection inside an embedded type come from the type, so two owners of the type collide') void "two entities can embed a type that has a collection"() { when: - HibernateDatastore datastore = new HibernateDatastore(EmbeddedCollectionOwnerA, EmbeddedCollectionOwnerB) + HibernateDatastore datastore = new HibernateDatastore(CLASSIC, EmbeddedCollectionOwnerA, EmbeddedCollectionOwnerB) then: notThrown(Exception) diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/jpa/GrailsDomainGeneratorDifferentialSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/jpa/GrailsDomainGeneratorDifferentialSpec.groovy index 07c110c9f1..dabc6f22b1 100644 --- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/jpa/GrailsDomainGeneratorDifferentialSpec.groovy +++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/jpa/GrailsDomainGeneratorDifferentialSpec.groovy @@ -460,7 +460,7 @@ class GrailsDomainGeneratorDifferentialSpec extends HibernateGormDatastoreSpec { } /** - * Boots a group of entities with the classic binder. GORM only gives an entity a tenant id (and the binder only adds the tenant filter) in + * Boots a group of entities with the classic binder (stated, since native binding is the default). GORM only gives an entity a tenant id (and the binder only adds the tenant filter) in * discriminator multi-tenancy mode, so a group with multi-tenant entities is booted in that mode when it can be. */ private static HibernateDatastore boot(List<Class<?>> group) { @@ -468,6 +468,7 @@ class GrailsDomainGeneratorDifferentialSpec extends HibernateGormDatastoreSpec { try { return new HibernateDatastore([ 'dataSource.dbCreate' : 'create-drop', + 'hibernate.generatedDomainClasses' : false, 'grails.gorm.multiTenancy.mode' : MultiTenancySettings.MultiTenancyMode.DISCRIMINATOR, 'grails.gorm.multiTenancy.tenantResolver': new SystemPropertyTenantResolver(), ], group as Class[]) @@ -475,7 +476,8 @@ class GrailsDomainGeneratorDifferentialSpec extends HibernateGormDatastoreSpec { // a domain that needs another configuration is booted by default below } } - return new HibernateDatastore(group as Class[]) + // the same create-drop the no-argument constructor applies, which resolves the SQL types the records hold + return new HibernateDatastore(['dataSource.dbCreate': 'create-drop', 'hibernate.generatedDomainClasses': false], group as Class[]) } /** diff --git a/grails-data-hibernate7/core/src/test/resources/classic-oracle/ddl-differential.txt b/grails-data-hibernate7/core/src/test/resources/classic-oracle/ddl-differential.txt index a66a4cd1bd..584d2f9b27 100644 --- a/grails-data-hibernate7/core/src/test/resources/classic-oracle/ddl-differential.txt +++ b/grails-data-hibernate7/core/src/test/resources/classic-oracle/ddl-differential.txt @@ -1546,6 +1546,10 @@ script "ALTER TABLE \"PUBLIC\".\"GDC_TENANT_AUTHOR\" ADD CONSTRAINT \"PUBLIC\". script "CREATE MEMORY TABLE \"PUBLIC\".\"GDC_TENANT_BOOK\"(\n \"AUTHOR_ID\" BIGINT,\n \"ID\" BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 1) NOT NULL,\n \"VERSION\" BIGINT NOT NULL,\n \"TITLE\" CHARACTER VARYING(255)\n);" script "ALTER TABLE \"PUBLIC\".\"GDC_TENANT_BOOK\" ADD CONSTRAINT \"PUBLIC\".\"CONSTRAINT_#\" PRIMARY KEY(\"ID\");" script "ALTER TABLE \"PUBLIC\".\"GDC_TENANT_BOOK\" ADD CONSTRAINT \"PUBLIC\".\"FKREGPA62G053JK0RFAO4IMN072\" FOREIGN KEY(\"AUTHOR_ID\") REFERENCES \"PUBLIC\".\"GDC_TENANT_AUTHOR\"(\"ID\") NOCHECK;" +@group grails.gorm.tests.generated.GdcTenantCompositeBook {"members":["grails.gorm.tests.generated.GdcTenantCompositeBook"]} +table gdc_tenant_composite_book {"checks":[],"collections":[],"columnOrder":["version","id","tenant_id","title"],"columns":{"id":{"checks":[],"collation":null,"comment":null,"default":null,"generatedAs":null,"identity":false,"nullable":false,"type":"varchar(255)","unique":false},"tenant_id":{"checks":[],"collation":null,"comment":null,"default":null,"generatedAs":null,"identity":false,"nullable":false,"type":"varchar(255)","unique":false},"title":{"checks":[],"collation":null,"comment":n [...] +script "CREATE MEMORY TABLE \"PUBLIC\".\"GDC_TENANT_COMPOSITE_BOOK\"(\n \"VERSION\" BIGINT NOT NULL,\n \"ID\" CHARACTER VARYING(255) NOT NULL,\n \"TENANT_ID\" CHARACTER VARYING(255) NOT NULL,\n \"TITLE\" CHARACTER VARYING(255)\n);" +script "ALTER TABLE \"PUBLIC\".\"GDC_TENANT_COMPOSITE_BOOK\" ADD CONSTRAINT \"PUBLIC\".\"CONSTRAINT_#\" PRIMARY KEY(\"ID\", \"TENANT_ID\");" @group grails.gorm.tests.generated.GdcTenantItem {"members":["grails.gorm.tests.generated.GdcTenantItem"]} table gdc_tenant_item {"checks":[],"collections":[],"columnOrder":["id","version","name","tenant_id"],"columns":{"id":{"checks":[],"collation":null,"comment":null,"default":null,"generatedAs":null,"identity":true,"nullable":false,"type":"bigint","unique":false},"name":{"checks":[],"collation":null,"comment":null,"default":null,"generatedAs":null,"identity":false,"nullable":true,"type":"varchar(255)","unique":false},"tenant_id":{"checks":[],"collation":null,"comment":null,"default":null," [...] script "CREATE MEMORY TABLE \"PUBLIC\".\"GDC_TENANT_ITEM\"(\n \"ID\" BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 1) NOT NULL,\n \"VERSION\" BIGINT NOT NULL,\n \"NAME\" CHARACTER VARYING(255),\n \"TENANT_ID\" CHARACTER VARYING(255)\n);" diff --git a/grails-data-hibernate7/core/src/test/resources/classic-oracle/generator-differential.txt b/grails-data-hibernate7/core/src/test/resources/classic-oracle/generator-differential.txt index b14f1f717d..355739310a 100644 --- a/grails-data-hibernate7/core/src/test/resources/classic-oracle/generator-differential.txt +++ b/grails-data-hibernate7/core/src/test/resources/classic-oracle/generator-differential.txt @@ -506,6 +506,9 @@ entity grails.gorm.tests.generated.GdcSerPet {"abstractClass":false,"batchSize": @group grails.gorm.tests.generated.GdcTenantAuthor {"members":["grails.gorm.tests.generated.GdcTenantAuthor","grails.gorm.tests.generated.GdcTenantBook"]} entity grails.gorm.tests.generated.GdcTenantAuthor {"abstractClass":false,"batchSize":0,"cached":false,"declaredNames":["version","name","books"],"discriminatorInsertable":true,"dynamicInsert":false,"dynamicUpdate":false,"entityName":"grails.gorm.tests.generated.GdcTenantAuthor","generator":{"className":"org.grails.orm.hibernate.cfg.domainbinding.generator.GrailsNativeGenerator"},"identifierKind":"BasicValue","identifierProperty":"id","joinTables":{},"jpaEntityName":"GdcTenantAuthor","ki [...] entity grails.gorm.tests.generated.GdcTenantBook {"abstractClass":false,"batchSize":0,"cached":false,"declaredNames":["version","title","author"],"discriminatorInsertable":true,"dynamicInsert":false,"dynamicUpdate":false,"entityName":"grails.gorm.tests.generated.GdcTenantBook","generator":{"className":"org.grails.orm.hibernate.cfg.domainbinding.generator.GrailsNativeGenerator"},"identifierKind":"BasicValue","identifierProperty":"id","joinTables":{},"jpaEntityName":"GdcTenantBook","kind": [...] +@group grails.gorm.tests.generated.GdcTenantCompositeBook {"members":["grails.gorm.tests.generated.GdcTenantCompositeBook"]} +entity grails.gorm.tests.generated.GdcTenantCompositeBook {"abstractClass":false,"batchSize":0,"cached":false,"declaredNames":["version","title"],"discriminatorInsertable":true,"dynamicInsert":false,"dynamicUpdate":false,"entityName":"grails.gorm.tests.generated.GdcTenantCompositeBook","identifier":{"embedded":true,"kind":"Component","members":[{"backref":false,"insertable":true,"lazy":false,"name":"id","naturalIdentifier":false,"optional":false,"updateable":true,"value":{"javaType":"jav [...] +filter tenantId {"appliedToLoadByKey":false,"autoEnabled":false,"name":"tenantId","parameterNames":["tenantId"],"parameterTypes":{"tenantId":"java.lang.String"}} @group grails.gorm.tests.generated.GdcTenantItem {"members":["grails.gorm.tests.generated.GdcTenantItem"]} entity grails.gorm.tests.generated.GdcTenantItem {"abstractClass":false,"batchSize":0,"cached":false,"declaredNames":["version","name","tenantId"],"discriminatorInsertable":true,"dynamicInsert":false,"dynamicUpdate":false,"entityName":"grails.gorm.tests.generated.GdcTenantItem","generator":{"className":"org.grails.orm.hibernate.cfg.domainbinding.generator.GrailsNativeGenerator"},"identifierKind":"BasicValue","identifierProperty":"id","joinTables":{},"jpaEntityName":"GdcTenantItem","kind" [...] filter tenantId {"appliedToLoadByKey":false,"autoEnabled":false,"name":"tenantId","parameterNames":["tenantId"],"parameterTypes":{"tenantId":"java.lang.String"}}
