Copilot commented on code in PR #16522: URL: https://github.com/apache/grails-core/pull/16522#discussion_r4185443833
########## grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/BasicCollectionKeyPersistenceSpec.groovy: ########## @@ -0,0 +1,75 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.orm.hibernate.cfg.domainbinding.secondpass + +import grails.gorm.annotation.Entity +import grails.gorm.tests.HibernateGormDatastoreSpec + +/** + * Regression test: {@code CollectionKeyColumnUpdater} made the key of a collection not updatable when its owner had + * more than one unidirectional to-many property. Hibernate then writes no rows for the collection, so the elements of a + * collection of basic values were silently lost. The key of a collection of basic values must stay updatable. + */ +class BasicCollectionKeyPersistenceSpec extends HibernateGormDatastoreSpec { + + void setupSpec() { + manager.registerDomainClasses(BCKPOneCollection, BCKPTwoCollections) + } + + void "the elements of the only collection of an owner are persisted"() { + given: + BCKPOneCollection owner = new BCKPOneCollection(name: 'one', tags: ['a', 'b'] as Set) + owner.save(flush: true, failOnError: true) + session.clear() + + expect: + BCKPOneCollection.get(owner.id).tags == ['a', 'b'] as Set + } + + void "the elements of each of two collections of an owner are persisted"() { + given: + BCKPTwoCollections owner = new BCKPTwoCollections(name: 'two', tags: ['a', 'b'] as Set, scores: [1, 2, 3]) + owner.save(flush: true, failOnError: true) + session.clear() + BCKPTwoCollections loaded = BCKPTwoCollections.get(owner.id) + + expect: + loaded.tags == ['a', 'b'] as Set + loaded.scores == [1, 2, 3] Review Comment: This spec is verifying that elements are persisted/reloaded (not ordering), but the `loaded.scores == [1, 2, 3]` assertion makes the test order-sensitive. Unless list ordering is explicitly part of the requirement being tested here, consider asserting contents/size instead to avoid flaky failures due to ordering differences in collection mappings. ########## grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionKeyColumnUpdater.java: ########## @@ -46,7 +47,9 @@ public void bind(HibernateToManyProperty property) { .filter(p -> !p.isBidirectional()) .count(); - key.setUpdateable(unidirectionalCount <= 1); + // The key of a collection of basic values lives in a table of its own and Hibernate writes no rows for a + // collection whose key is not updatable, so only the key of an entity collection is ever made non-updatable. Review Comment: The comment doesn’t clearly match the condition being applied. If `HibernateToManyCollectionProperty` represents *basic/enumeration collections* (as the PR description implies), then the comment’s last clause (“only the key of an entity collection…”) is misleading—this line is explicitly special-casing a property type. Please update the comment to precisely describe which property type(s) remain updatable and which case(s) become non-updatable (and why), so future changes don’t invert the logic accidentally. -- 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]
