aulea opened a new issue, #14460:
URL: https://github.com/apache/grails-core/issues/14460

   Given the following Domain class
   ```
   class A {
       String a
       E efs
   
       static embedded = [ 'efs' ]
   
       static constraints = {
             a validator: { String newValue, A obj ->
                   return ['not.valid.a']
             }
       }
   }
   ```
   and Embedded class
   ```
   class E {
       String f
   }
   ```
   ### Steps to Reproduce
   
   When trying to save new instance of domain A with validation error, it fails 
instead with IllegalArgumentException from Hibernate telling that embedded 
class E is not an entity. Of course that's true, but i would expect to get 
original ValidationException.
   
   Originally posted under grails-data-mapping 
[(1199)](https://github.com/grails/grails-data-mapping/issues/1199)
   
   The Stacktrace:
   ```
   java.lang.IllegalArgumentException: Not an entity [class ...]
        at org.hibernate.internal.SessionImpl.contains(SessionImpl.java:2068)
        at 
org.grails.orm.hibernate.cfg.GrailsHibernateUtil.canModifyReadWriteState(GrailsHibernateUtil.java:316)
        at 
org.grails.orm.hibernate.cfg.GrailsHibernateUtil.setObjectToReadyOnly(GrailsHibernateUtil.java:305)
        at 
org.grails.orm.hibernate.HibernateGormInstanceApi.setObjectToReadOnly(HibernateGormInstanceApi.groovy:159)
        at 
org.grails.orm.hibernate.AbstractHibernateGormInstanceApi.handleValidationError(AbstractHibernateGormInstanceApi.groovy:397)
        at 
org.grails.orm.hibernate.AbstractHibernateGormInstanceApi.save(AbstractHibernateGormInstanceApi.groovy:132)
        at 
org.grails.datastore.gorm.GormEntity$Trait$Helper.save(GormEntity.groovy:151)
   ```
   
   The reason is, that gorm tries to set objects with failure to read-only 
state in hibernate session, but doesn't distinct correctly Embedded objects:
   
[AbstractHibernateGormInstanceApi#handleValidationError](https://github.com/grails/gorm-hibernate5/blob/master/grails-datastore-gorm-hibernate5/src/main/groovy/org/grails/orm/hibernate/AbstractHibernateGormInstanceApi.groovy#L392-L410)
   It loops over all domain's associations and check their instance type ToOne, 
it should exclude instance type Embedded.
   
   ### Expected Behaviour
   
   Should fail with ValidationException for validated field
   
   ### Actual Behaviour
   
   Fails with IllegalArgumentException from Hibernate: Not an Entity [class E]
   
   ### Environment Information
   
   - **Operating System**: Windows
   - **GORM Version:** 6.1.10
   - **Grails Version (if using Grails):** 3.3.8
   - **JDK Version:** 8
   
   
   Workaround is to use manual validation before save and throw by youself new 
ValidationException created from validated domain errors.
   
   ```
   A a = new A()
   a.a = 'string'
   a.efs = new E(f: 'string')
   
   if (!a.validate()) {
         throw new ValidationException("Validation Error(s) occurred during 
save()", a.errors)
   }
   a.save(flush: true)
   ```
   
   


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