1

我正在从 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 中运行良好

任何我可以寻求修复错误的建议将不胜感激。

4

3 回答 3

0

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

 at UserManage.User.save(User.groovy)

以我的经验,Grails 3 比 Grails 2 更安全,而且对歧义的容忍度更低。明确:User a = new User()

于 2017-01-10T22:33:58.343 回答
0

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

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

于 2017-01-11T09:58:36.347 回答
0

只是添加我对这个主题的注释,因为这篇文章让我把很多注意力放在导致问题的错误方面,希望节省其他人花时间尝试识别似乎没有正确定义的东西。

input a null value for a date that is not specified

这并不完全正确。

我花了很长时间试图跟踪问题是在哪里创建的 3.2.8 下的一个虚拟站点,并试图重新创建 hasMany 和具有可为空的真实对象的实际对象。如果是这种情况,那么需要大量额外的工作来填充不需要的空值,并最终用不需要的东西填充 DB 并破坏代码的其他逻辑。

据我所知,无论哪种方式,到目前为止,这个问题都是由 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)

于 2017-04-23T18:02:41.537 回答