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


##########
grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/GormInstanceApi.groovy:
##########
@@ -273,7 +276,27 @@ class GormInstanceApi<D> extends AbstractGormApi<D> 
implements GormInstanceOpera
 
     @Override
     Serializable ident(D instance) {
-        (Serializable)InvokerHelper.getProperty(instance, 'id')
+        PersistentEntity entity = 
mappingContext.getPersistentEntity(persistentClass.name)
+        if (entity == null) {
+            return (Serializable) InvokerHelper.getProperty(instance, 'id')
+        }
+        PersistentProperty identity = entity.identity
+        if (identity != null) {
+            return (Serializable) InvokerHelper.getProperty(instance, 
identity.name)
+        }
+
+        PersistentProperty[] idProperties = entity.compositeIdentity
+        if (idProperties != null) {
+            def identifier = entity.newInstance()
+            if (identifier instanceof Serializable) {
+                EntityReflector reflector = entity.reflector
+                for (PersistentProperty property : idProperties) {
+                    reflector.setProperty(identifier, property.name, 
reflector.getProperty(instance, property.name))

Review Comment:
   Fixed in 84314f346c. `ident()` now returns `ProxyHandler.getIdentifier()` 
for an uninitialized proxy before reading any mapped property, so this also 
covers the composite-key path.
   
   `GormInstanceApiIdentSpec` loads uninitialized SimpleMap (Javassist) proxies 
for a generated key and for a renamed key whose row does not exist. It asserts 
that `ident()` returns the key and that the proxy stays uninitialized. Before 
the fix, the renamed-key case failed with `DataIntegrityViolationException: 
Proxy for [IdentNamed:MISSING] could not be initialized`. Another feature 
checks that an initialized renamed-key proxy still returns the mapped `code` 
value.



##########
grails-doc/src/en/ref/Domain Classes/ident.adoc:
##########
@@ -25,7 +25,9 @@ under the License.
 === Purpose
 
 
-Returns the value of the identity property of the domain class regardless of 
the name of the identity property itself
+Returns the value of the mapped identity property of the domain class, 
regardless of its name. For example, when the mapping uses `id name: 'code'`, 
`ident()` returns the value of `code`. A generated identifier remains `null` 
until it has been assigned.
+
+For a composite identity, `ident()` returns a separate serializable instance 
of the domain class with the mapped key properties copied into it, including 
any associations that form part of the key. Other properties retain their 
new-instance defaults. Pass the returned identifier to `get()` or `load()` to 
retrieve the entity.

Review Comment:
   Updated in 84314f346c. The composite paragraph now describes an 
uninitialized proxy, such as one returned by `load()`, as the exception: 
`ident()` returns the identifier the proxy was created with and does not load 
the entity. For a composite key, that is the object passed to `load()`, 
including any non-key properties set on it.
   
   A new feature in the Hibernate 5 and Hibernate 7 `MappedIdentifierSpec` 
asserts that `load(key).ident()` returns the same `key` object, that its 
non-key property is kept, and that the proxy stays uninitialized.



##########
grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/proxy/ByteBuddyGroovyInterceptorSpec.groovy:
##########
@@ -142,15 +142,14 @@ class ByteBuddyGroovyInterceptorSpec extends 
HibernateGormDatastoreSpec {
         !Hibernate.isInitialized(proxy)
     }
 
-    void "getIdentifier() on uninitialized proxy returns identifier without 
initialization"() {
+    void "a user-defined getIdentifier() invokes the entity implementation"() {
         given:
         def proxy = manager.hibernateSession.getReference(Location, savedId)

Review Comment:
   Switched to `Location.load(savedId)` in 84314f346c.



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