1

我创建了一个 Grails 3.1.0 项目,其中包含 5 个域类 3 个域是 Spring Security Plugin 中的 User、Role 和 UserRole,另外 2 个是一对多的关系。

我正在连接到 MySQL 5.6,并且我的 dbCreate 设置为更新。

使用干净的数据库启动项目时,会创建表并且一切都很好,但是当我使用已创建表的数据库启动项目时,启动失败并出现 NullPointerException。

每次运行我的应用程序时,我都需要删除并创建数据库。

堆栈跟踪:

ERROR grails.boot.GrailsApp - Application startup failed
java.lang.NullPointerException: null
at org.hibernate.type.descriptor.java.AbstractTypeDescriptor.extractHashCode(AbstractTypeDescriptor.java:84) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.type.AbstractStandardBasicType.getHashCode(AbstractStandardBasicType.java:216) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.type.AbstractStandardBasicType.getHashCode(AbstractStandardBasicType.java:220) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.type.EntityType.getHashCode(EntityType.java:391) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.type.ComponentType.getHashCode(ComponentType.java:258) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.engine.spi.EntityKey.generateHashCode(EntityKey.java:76) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.engine.spi.EntityKey.<init>(EntityKey.java:71) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.internal.AbstractSessionImpl.generateEntityKey(AbstractSessionImpl.java:327) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:175) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:138) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:715) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:707) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:702) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.grails.orm.hibernate.AbstractHibernateGormInstanceApi$_performInsert_closure5.doCall(AbstractHibernateGormInstanceApi.groovy:256) ~[grails-datastore-gorm-hibernate-core-5.0.0.RELEASE.jar:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_91]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_91]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_91]
    at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_91]
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1426) ~[springloaded-1.2.5.RELEASE.jar:1.2.5.RELEASE]
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93) ~[groovy-2.4.5.jar:2.4.5]
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325) ~[groovy-2.4.5.jar:2.4.5]
    at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:294) ~[groovy-2.4.5.jar:2.4.5]
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1019) ~[groovy-2.4.5.jar:2.4.5]
    at groovy.lang.Closure.call(Closure.java:426) ~[groovy-2.4.5.jar:2.4.5]
    at org.codehaus.groovy.runtime.ConvertedClosure.invokeCustom(ConvertedClosure.java:53) ~[groovy-2.4.5.jar:2.4.5]
    at org.codehaus.groovy.runtime.ConversionHandler.invoke(ConversionHandler.java:105) ~[groovy-2.4.5.jar:2.4.5]
    at com.sun.proxy.$Proxy93.doInHibernate(Unknown Source) ~[na:na]
    at org.grails.orm.hibernate.GrailsHibernateTemplate.doExecute(GrailsHibernateTemplate.java:196) ~[grails-datastore-gorm-hibernate4-5.0.0.RELEASE.jar:na]
    at org.grails.orm.hibernate.GrailsHibernateTemplate.execute(GrailsHibernateTemplate.java:140) ~[grails-datastore-gorm-hibernate4-5.0.0.RELEASE.jar:na]
    at org.grails.orm.hibernate.GrailsHibernateTemplate.execute(GrailsHibernateTemplate.java:110) ~[grails-datastore-gorm-hibernate4-5.0.0.RELEASE.jar:na]
    at org.grails.orm.hibernate.AbstractHibernateGormInstanceApi.performInsert(AbstractHibernateGormInstanceApi.groovy:253) ~[grails-datastore-gorm-hibernate-core-5.0.0.RELEASE.jar:na]
    at org.grails.orm.hibernate.AbstractHibernateGormInstanceApi.save(AbstractHibernateGormInstanceApi.groovy:146) ~[grails-datastore-gorm-hibernate-core-5.0.0.RELEASE.jar:na]
    at org.grails.datastore.gorm.GormEntity$Trait$Helper.save(GormEntity.groovy:140) ~[grails-datastore-gorm-5.0.0.RELEASE.jar:na]
    at org.grails.datastore.gorm.GormEntity$Trait$Helper$save$2.call(Unknown Source) ~[na:na]
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48) [groovy-2.4.5.jar:2.4.5]
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113) [groovy-2.4.5.jar:2.4.5]
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133) [groovy-2.4.5.jar:2.4.5]
    at com.equineedge.photostore.AuthUserAuthRole.save(AuthUserAuthRole.groovy) ~[main/:na]
    at com.equineedge.photostore.AuthUserAuthRole.save(AuthUserAuthRole.groovy) ~[main/:na]
    at org.grails.datastore.gorm.GormEntity$save.call(Unknown Source) ~[na:na]
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48) [groovy-2.4.5.jar:2.4.5]
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113) [groovy-2.4.5.jar:2.4.5]
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125) [groovy-2.4.5.jar:2.4.5]
    at com.equineedge.photostore.AuthUserAuthRole.create(AuthUserAuthRole.groovy:56) ~[main/:na]
    at com.equineedge.photostore.AuthUserAuthRole$create.call(Unknown Source) ~[na:na]
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48) [groovy-2.4.5.jar:2.4.5]
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113) [groovy-2.4.5.jar:2.4.5]
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:141) [groovy-2.4.5.jar:2.4.5]
    at BootStrap.createDummyUsers(BootStrap.groovy:16) ~[main/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_91]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_91]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_91]
    at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_91]
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1426) ~[springloaded-1.2.5.RELEASE.jar:1.2.5.RELEASE]
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93) ~[groovy-2.4.5.jar:2.4.5]
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325) ~[groovy-2.4.5.jar:2.4.5]
    at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:384) ~[groovy-2.4.5.jar:2.4.5]
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1019) ~[groovy-2.4.5.jar:2.4.5]
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:69) ~[groovy-2.4.5.jar:2.4.5]
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52) [groovy-2.4.5.jar:2.4.5]
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154) [groovy-2.4.5.jar:2.4.5]
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:158) [groovy-2.4.5.jar:2.4.5]
    at BootStrap$_closure1.doCall(BootStrap.groovy:6) ~[main/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_91]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_91]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_91]
    at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_91]
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1426) ~[springloaded-1.2.5.RELEASE.jar:1.2.5.RELEASE]
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93) ~[groovy-2.4.5.jar:2.4.5]
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325) ~[groovy-2.4.5.jar:2.4.5]
    at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:294) ~[groovy-2.4.5.jar:2.4.5]
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1019) ~[groovy-2.4.5.jar:2.4.5]
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1084) ~[groovy-2.4.5.jar:2.4.5]
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1019) ~[groovy-2.4.5.jar:2.4.5]
    at groovy.lang.Closure.call(Closure.java:426) ~[groovy-2.4.5.jar:2.4.5]
    at groovy.lang.Closure.call(Closure.java:420) ~[groovy-2.4.5.jar:2.4.5]
    at grails.util.Environment.evaluateEnvironmentSpecificBlock(Environment.java:419) ~[grails-bootstrap-3.1.0.jar:3.1.0]
    at grails.util.Environment.executeForEnvironment(Environment.java:412) ~[grails-bootstrap-3.1.0.jar:3.1.0]
    at grails.util.Environment.executeForCurrentEnvironment(Environment.java:388) ~[grails-bootstrap-3.1.0.jar:3.1.0]
    at org.grails.web.servlet.boostrap.DefaultGrailsBootstrapClass.callInit(DefaultGrailsBootstrapClass.java:62) ~[grails-web-3.1.0.jar:3.1.0]
    at org.grails.web.servlet.context.GrailsConfigUtils.executeGrailsBootstraps(GrailsConfigUtils.java:65) ~[grails-web-3.1.0.jar:3.1.0]
    at org.grails.plugins.web.servlet.context.BootStrapClassRunner.onStartup(BootStrapClassRunner.groovy:53) ~[grails-plugin-controllers-3.1.0.jar:3.1.0]
    at grails.boot.config.GrailsApplicationPostProcessor.onApplicationEvent(GrailsApplicationPostProcessor.groovy:238) ~[grails-core-3.1.0.jar:3.1.0]
    at grails.boot.config.GrailsApplicationPostProcessor.onApplicationEvent(GrailsApplicationPostProcessor.groovy) ~[grails-core-3.1.0.jar:3.1.0]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:163) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:136) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:381) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:335) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:855) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:140) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:764) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
    at org.springframework.boot.SpringApplication.doRun(SpringApplication.java:357) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:305) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
    at grails.boot.GrailsApp.run(GrailsApp.groovy:54) [grails-core-3.1.0.jar:3.1.0]
    at grails.boot.GrailsApp.run(GrailsApp.groovy:353) [grails-core-3.1.0.jar:3.1.0]
    at grails.boot.GrailsApp.run(GrailsApp.groovy:342) [grails-core-3.1.0.jar:3.1.0]
    at grails.boot.GrailsApp$run.call(Unknown Source) [grails-core-3.1.0.jar:3.1.0]
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48) [groovy-2.4.5.jar:2.4.5]
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113) [groovy-2.4.5.jar:2.4.5]
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133) [groovy-2.4.5.jar:2.4.5]
    at photostore.Application.main(Application.groovy:8) [main/:na]

createDummyUsers() 函数

private createDummyUsers(){
    //System Administrator User and Role
    AuthUser adminUser = AuthUser.findOrSaveWhere(username: 'admin', password: 'adminPass')
    AuthRole adminRole = AuthRole.findOrSaveWhere(authority: 'ROLE_ADMIN')

    AuthUserAuthRole.create adminUser, adminRole, true <-- LINE 16

    //API User and Role
    AuthUser apiUser = AuthUser.findOrSaveWhere(username: 'api', password: 'apiPass')
    AuthRole apiRole = AuthRole.findOrSaveWhere(authority: 'ROLE_API')

    AuthUserAuthRole.create apiUser, apiRole, true

    //User User and Role
    AuthUser userUser = AuthUser.findOrSaveWhere(username: 'user', password: 'userPass')
    AuthRole userRole = AuthRole.findOrSaveWhere(authority: 'ROLE_USER')

    AuthUserAuthRole.create userUser, userRole, true

    //All Roles User
    AuthUser rootUser = AuthUser.findOrSaveWhere(username: 'root', password: 'rootPass')

    AuthUserAuthRole.create rootUser, adminRole, true
    AuthUserAuthRole.create rootUser, apiRole, true
    AuthUserAuthRole.create rootUser, userRole, true
} 

由安全插件生成的 AuthUserAuthRole 域

package com.equineedge.photostore

import grails.gorm.DetachedCriteria
import groovy.transform.ToString
import org.apache.commons.lang.builder.HashCodeBuilder

@ToString(cache=true, includeNames=true, includePackage=false)
class AuthUserAuthRole implements Serializable {

private static final long serialVersionUID = 1

AuthUser authUser
AuthRole authRole

AuthUserAuthRole(AuthUser u, AuthRole r) {
    this()
    authUser = u
    authRole = r
}

@Override
boolean equals(other) {
    if (!(other instanceof AuthUserAuthRole)) {
        return false
    }

    other.authUser?.id == authUser?.id && other.authRole?.id == authRole?.id
}

@Override
int hashCode() {
    def builder = new HashCodeBuilder()
    if (authUser) builder.append(authUser.id)
    if (authRole) builder.append(authRole.id)
    builder.toHashCode()
}

static AuthUserAuthRole get(long authUserId, long authRoleId) {
    criteriaFor(authUserId, authRoleId).get()
}

static boolean exists(long authUserId, long authRoleId) {
    criteriaFor(authUserId, authRoleId).count()
}

private static DetachedCriteria criteriaFor(long authUserId, long authRoleId) {
    AuthUserAuthRole.where {
        authUser == AuthUser.load(authUserId) &&
        authRole == AuthRole.load(authRoleId)
    }
}

static AuthUserAuthRole create(AuthUser authUser, AuthRole authRole, boolean flush = false) {
    def instance = new AuthUserAuthRole(authUser: authUser, authRole: authRole)
    instance.save(flush: flush, insert: true)
    instance
}

static boolean remove(AuthUser u, AuthRole r, boolean flush = false) {
    if (u == null || r == null) return false

    int rowCount = AuthUserAuthRole.where { authUser == u && authRole == r }.deleteAll()

    if (flush) { AuthUserAuthRole.withSession { it.flush() } }

    rowCount
}

static void removeAll(AuthUser u, boolean flush = false) {
    if (u == null) return

    AuthUserAuthRole.where { authUser == u }.deleteAll()

    if (flush) { AuthUserAuthRole.withSession { it.flush() } }
}

static void removeAll(AuthRole r, boolean flush = false) {
    if (r == null) return

    AuthUserAuthRole.where { authRole == r }.deleteAll()

    if (flush) { AuthUserAuthRole.withSession { it.flush() } }
}

static constraints = {
    authRole validator: { AuthRole r, AuthUserAuthRole ur ->
        if (ur.authUser == null || ur.authUser.id == null) return
        boolean existing = false
        AuthUserAuthRole.withNewSession {
            existing = AuthUserAuthRole.exists(ur.authUser.id, r.id)
        }
        if (existing) {
            return 'userRole.exists'
        }
    }
}

static mapping = {
    id composite: ['authUser', 'authRole']
    version false
}
}
4

1 回答 1

1

查看这篇文章如何将休眠升级到 5.x。

在这里您可以检查实际的休眠版本。

于 2016-02-09T09:47:02.657 回答