Abraham
Abraham

Reputation: 11

Grails 3.2.4 refuses to save domain instance: java.lang.IllegalArgumentException: object is not an instance of declaring class

I am upgrading from Grails 2.4.4 to Grails 3.2.4.

I have setup a few factories to help me with testing, the role factory works fine, which runs before the users get created. Then I have the following code:

        println "create user."
        def testUser = new User(username: '[email protected]', firstName: "admin", lastName: "admin", email: "[email protected]")
        println "User. :: " + testUser
        println "User class. :: " + testUser.getClass().toString()
        println("User Errors: " + testUser.errors)
        testUser.save()
        println "create user2."
        def testUser2 = new User(username: '[email protected]', email: '[email protected]', firstName: "trade", lastName: "trade").save()

Of which the output is:

create user
User. :: Person: [email protected]
User class. :: class UserManage.User
User Errors: grails.validation.ValidationErrors: 0 errors

2017-01-10 10:21:18.400 ERROR --- [           main] o.s.boot.SpringApplication               : Application startup failed

java.lang.IllegalArgumentException: object is not an instance of declaring class
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1426)
        at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
        at groovy.lang.MetaBeanProperty.getProperty(MetaBeanProperty.java:62)
        at groovy.lang.MetaClassImpl.invokePropertyOrMissing(MetaClassImpl.java:1245)
        at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1217)
        at groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:1125)
        at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1024)
        at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:812)
        at org.grails.validation.ConstrainedPropertyBuilder.doInvokeMethod(ConstrainedPropertyBuilder.java:74)
        at groovy.util.BuilderSupport.invokeMethod(BuilderSupport.java:67)
        at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeOnDelegationObjects(ClosureMetaClass.java:446)
        at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:369)
        at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1024)
        at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:69)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:166)
        at UserManage.User$__clinit__closure2.doCall(User.groovy:79)
        at UserManage.User$__clinit__closure2.doCall(User.groovy)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1426)
        at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
        at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
        at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:294)
        at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1024)
        at groovy.lang.Closure.call(Closure.java:414)
        at UserManage.User$__clinit__closure2.call(User.groovy)
        at groovy.lang.Closure.call(Closure.java:408)
        at UserManage.User$__clinit__closure2.call(User.groovy)
        at org.grails.validation.DefaultConstraintEvaluator.evaluateConstraintsMap(DefaultConstraintEvaluator.java:240)
        at org.grails.validation.DefaultConstraintEvaluator.evaluateConstraints(DefaultConstraintEvaluator.java:132)
        at org.grails.validation.DefaultConstraintEvaluator.evaluateConstraints(DefaultConstraintEvaluator.java:119)
        at org.grails.validation.DefaultConstraintEvaluator.evaluate(DefaultConstraintEvaluator.java:108)
        at org.grails.core.DefaultGrailsDomainClass.initializeConstraints(DefaultGrailsDomainClass.java:755)
        at org.grails.core.DefaultGrailsDomainClass.getConstrainedProperties(DefaultGrailsDomainClass.java:746)
        at org.grails.validation.GrailsDomainClassValidator.validate(GrailsDomainClassValidator.java:76)
        at org.grails.orm.hibernate.AbstractHibernateGormInstanceApi.save(AbstractHibernateGormInstanceApi.groovy:122)
        at org.grails.datastore.gorm.GormInstanceApi.save(GormInstanceApi.groovy:116)
        at org.grails.datastore.gorm.GormEntity$Trait$Helper.save(GormEntity.groovy:98)
        at org.grails.datastore.gorm.GormEntity$Trait$Helper$save$5.call(Unknown Source)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
        at UserManage.User.save(User.groovy)
        at UserManage.User.save(User.groovy)
        at org.grails.datastore.gorm.GormEntity$save$0.call(Unknown Source)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
        at groovy.Factories.UserFactory.Build(UserFactory.groovy:18)
        at groovy.Factories.UserFactory$Build.call(Unknown Source)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
        at agripedia.BootStrap$_closure1.doCall(BootStrap.groovy:24)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1426)
        at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
        at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
        at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:294)
        at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1024)
        at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1089)
        at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1024)
        at groovy.lang.Closure.call(Closure.java:414)
        at groovy.lang.Closure.call(Closure.java:408)
        at grails.util.Environment.evaluateEnvironmentSpecificBlock(Environment.java:516)
        at grails.util.Environment.executeForEnvironment(Environment.java:509)
        at grails.util.Environment.executeForCurrentEnvironment(Environment.java:485)
        at org.grails.web.servlet.boostrap.DefaultGrailsBootstrapClass.callInit(DefaultGrailsBootstrapClass.java:62)
        at org.grails.web.servlet.context.GrailsConfigUtils.executeGrailsBootstraps(GrailsConfigUtils.java:65)
        at org.grails.plugins.web.servlet.context.BootStrapClassRunner.onStartup(BootStrapClassRunner.groovy:53)
        at grails.boot.config.GrailsApplicationPostProcessor.onApplicationEvent(GrailsApplicationPostProcessor.groovy:256)
        at grails.boot.config.GrailsApplicationPostProcessor.onApplicationEvent(GrailsApplicationPostProcessor.groovy)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:166)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:138)
        at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:383)
        at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:337)
        at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:882)
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:144)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:545)
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761)
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:371)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
        at grails.boot.GrailsApp.run(GrailsApp.groovy:83)
        at grails.boot.GrailsApp.run(GrailsApp.groovy:387)
        at grails.boot.GrailsApp.run(GrailsApp.groovy:374)
        at grails.boot.GrailsApp$run.call(Unknown Source)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133)
        at agripedia.Application.main(Application.groovy:8)

The save method fails, and it is not due to validation errors.

My class structure is APEntity as an abstract class and the User class inherits from the APEntity class. This code works fine in Grails 2.4.4

Any suggestions where I can look to fix the error would be greatly appreciated.

Upvotes: 1

Views: 1026

Answers (3)

V H
V H

Reputation: 8587

Just to add my notes on this topic since this was the post that made me put a lot of focus in the wrong aspects causing the issue, in the hope to save others spending time trying to identify something that doesn't seem correctly defined.

input a null value for a date that is not specified

This isn't strictly true.

I spent ages attempting to track where the issue was created a dummy site under 3.2.8 and tried to recreate the hasMany and actual objects that had nullable true. If this had been the case then a lot of additional work to fill in nulls where not needed and ultimately filling DB with stuff not required and breaking other logics of the code.

Either way so far as I understand this issue is caused in grails 3.2 by two things so far :

https://github.com/grails/grails-core/issues/10428

This was when a domain class object had

String aUser 

static constraints = { 
 aUser(nullable:true)
}

The above is already fixed in later versions of 3.2.X

A new issue hit recently https://github.com/grails/grails-core/issues/10600 when a domain object has _underscore

String _user 

static constraints = { 
 _user(nullable:true)
}

This appears to be in for fixing and should hopefully be fixed for 3.9

Meaning if you have _objects it will be safe to say it is likely to break under 3.2.X > - < 3.2.8 (was working in 3.1.10).

Upvotes: 0

Abraham
Abraham

Reputation: 11

It turned out that validation was the problem.

After I ensured that the types were correct, I disabled all of my constraints. Once the constraints were turned off the validation problem became apparent. In my situation, it was that in Grails 2 I would simply input a null value for a date that is not specified. In grails 3 it seems that this is not allowed, I thus gave it a default value and the validation problem was fixed.

Upvotes: 0

Anatoly
Anatoly

Reputation: 456

The class instance being constructed is User but the type seen is UserManage.User

 at UserManage.User.save(User.groovy)

In my experience Grails 3 is much more type-safe than Grails 2 and less forgiving for ambiguity. Be explicit: User a = new User()

Upvotes: 0

Related Questions