jdaugherty commented on code in PR #16627:
URL: https://github.com/apache/grails-core/pull/16627#discussion_r4297848614


##########
grails-data-hibernate5/core/src/test/groovy/org/grails/orm/hibernate/access/TraitPropertyFieldAccessSpec.groovy:
##########
@@ -0,0 +1,55 @@
+/*
+ *  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.access
+
+import org.hibernate.property.access.spi.PropertyAccess
+import spock.lang.Issue
+import spock.lang.Specification
+
+/**
+ * Regression test for private trait-field access through {@link 
TraitPropertyAccessStrategy}.
+ *
+ * Groovy implements trait properties by generating a private, name-mangled 
field on the
+ * implementing class (for example {@code org_example_MyTrait__value}) plus 
public bridge
+ * accessors. {@link TraitPropertyAccessStrategy} resolves the property to 
that generated
+ * field and builds a field-based {@code Getter}/{@code Setter} over it, so 
the field must
+ * be made accessible before it is used.
+ */
+class TraitPropertyFieldAccessSpec extends Specification {
+
+    @Issue('https://github.com/apache/grails-core/issues/16615')
+    void "field-based access to a trait property reads back what the setter 
wrote"() {

Review Comment:
   Added in 46d9c54d4a as `TraitPropertyRuntimeConnectionSourceSpec`; verified 
on both modules that it fails with the fix reverted and passes with it.



##########
grails-data-hibernate5/core/src/test/groovy/grails/gorm/tests/traits/TraitPropertyRuntimeConnectionSourceSpec.groovy:
##########
@@ -0,0 +1,79 @@
+/*
+ *  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 grails.gorm.tests.traits
+
+import grails.gorm.annotation.Entity
+import org.grails.datastore.gorm.GormEnhancer
+import org.grails.datastore.mapping.core.DatastoreUtils
+import org.grails.orm.hibernate.HibernateDatastore
+import org.springframework.util.ReflectionUtils
+import spock.lang.AutoCleanup
+import spock.lang.Issue
+import spock.lang.Shared
+import spock.lang.Specification
+
+import jakarta.persistence.AccessType
+
+/**
+ * GORM-flow coverage for the runtime path that reaches
+ * {@link org.grails.orm.hibernate.access.TraitPropertyAccessStrategy}: Spring 
clears its reflection
+ * cache at the end of every context refresh, so a SessionFactory built 
afterwards (a connection
+ * source added at runtime) sees an inaccessible generated trait field.
+ */
+class TraitPropertyRuntimeConnectionSourceSpec extends Specification {
+
+    @Shared @AutoCleanup HibernateDatastore datastore = new 
HibernateDatastore(DatastoreUtils.createPropertyResolver([
+            'dataSource.url'        : 
'jdbc:h2:mem:traitFieldRuntime;LOCK_TIMEOUT=10000',
+            'dataSource.dbCreate'   : 'create-drop',
+            'hibernate.hbm2ddl.auto': 'create-drop']), RuntimeTraitEntity)
+
+    @Issue('https://github.com/apache/grails-core/issues/16615')
+    void "a trait property is readable on a connection source added after 
Spring cleared its reflection cache"() {
+        given: 'what Spring does at the end of every context refresh'
+        ReflectionUtils.clearCache()
+
+        when: 'a data source added at runtime builds a new SessionFactory'
+        datastore.connectionSources.addConnectionSource('later', [pooled: 
true, dbCreate: 'create-drop',
+                url: 'jdbc:h2:mem:traitFieldLater;LOCK_TIMEOUT=10000'])
+        Long id = null
+        RuntimeTraitEntity.later.withTransaction {
+            def entity = new RuntimeTraitEntity(name: 'n', bar: 'b')
+            GormEnhancer.findInstanceApi(RuntimeTraitEntity, 
'later').save(entity, [flush: true, failOnError: true])

Review Comment:
   Optional, and this one is on me since the snippet I posted used it: 
`GormEnhancer.findInstanceApi` is not needed here. On 8.0.x an instance saved 
inside a named connection's own transaction is written to that connection 
(`MultipleDataSourceConnectionsSpec` covers it: "an instance saved inside a 
named connection's own transaction is written to that connection"), so this can 
be the plain end-user call:
   
   ```groovy
   entity.save(flush: true, failOnError: true)
   ```
   
   and the `GormEnhancer` import goes away. I ran both specs that way and they 
pass; the read-back through `RuntimeTraitEntity.later.get(id)` confirms the row 
lands on `later`. Same for the identical Hibernate 7 copy.



-- 
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