Grails 3.2.4拒绝保存域实例:java.lang.IllegalArgumentException:object不是声明类的实例

时间:2017-01-10 08:28:17

标签: java grails grails3

我正在从Grails 2.4.4升级到Grails 3.2.4。

我已经设置了一些工厂来帮助我进行测试,角色工厂工作正常,它在用户创建之前运行。然后我有以下代码:

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

其中输出为:

create user
User. :: Person: admin@mail.com
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)

保存方法失败,并不是由于验证错误。

我的类结构是APEntity作为抽象类,User类继承自APEntity类。此代码在Grails 2.4.4中运行良好

我将非常感谢任何可以解决错误的建议。

3 个答案:

答案 0 :(得分:0)

正在构建的类实例是User,但看到的类型是UserManage.User

 at UserManage.User.save(User.groovy)

根据我的经验,Grails 3比Grails 2更加类型安全,对模糊性的宽容度更低。明确:User a = new User()

答案 1 :(得分:0)

事实证明验证是问题所在。

在确保类型正确后,我禁用了所有约束。一旦关闭约束,验证问题就变得明显了。在我的情况下,在Grails 2中,我只是为未指定的日期输入空值。在grails 3中似乎不允许这样做,因此我给它一个默认值并且修复了验证问题。

答案 2 :(得分:0)

只是为了添加关于这个主题的笔记,因为这个帖子让我把重点放在导致问题的错误方面,希望能节省其他人花时间试图找出不具备的东西。看似正确定义。

input a null value for a date that is not specified

这并非严格要求。

我花了很多年时间试图跟踪问题在3.2.8下创建虚拟站点的位置,并尝试重新创建具有可为空的真实的hasMany和实际对象。如果是这种情况,则需要进行大量额外的工作来填充不需要的空值,并最终用不需要的东西填充数据库,并打破代码的其他逻辑。

无论哪种方式,据我所知,这个问题是由grails 3.2引起的,到目前为止有两件事:

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

这是一个域类对象

String aUser 

static constraints = { 
 aUser(nullable:true)
}

以上版本已在3.2.X

的更高版本中修复

当域对象具有_underscore

时,最近出现了一个新问题https://github.com/grails/grails-core/issues/10600
String _user 

static constraints = { 
 _user(nullable:true)
}

这似乎是为了修复,有希望修复为3.9

如果您拥有_objects,则可以安全地说它可能会在3.2.X > - < 3.2.8 (was working in 3.1.10)下破裂。