POJO设置程序导致的Grails 3 GORM多对多Hibernate映射错误

时间:2018-09-08 00:14:50

标签: hibernate gorm grails3

我有很多对很多的关系,它们忽略了hasMany和belongsTo关键字。相反,我们使用@burtbeckwith的性能建议,在此处进行说明:https://www.youtube.com/watch?v=-nofscHeEuU和此处:https://www.infoq.com/presentations/GORM-Performance

https://stackoverflow.com/users/160313/burt-beckwith

多对多关系是单向的,并且是唯一的,因为Workout对象上有3个属性可以保留相同类型的集合。通过在链接表中引入标识符,可以做到这一点。 (WorkoutMovementSet.groupIdentifier)。此解决方案在Grails 2.3.11中效果很好

class Workout extends BaseDomain{

    List<MovementSet> getMovementSets(){
        def joinedEntities = WorkoutMovementSet.findAllByWorkoutAndGroupIdentifier(this, null)
        return joinedEntities.sort { it.setIndex }.collect { it.movementSet } as List;
    }

    void setMovementSets(List<MovementSet> sets){
        sets.eachWithIndex { movementSet, index ->
            WorkoutMovementSet.create(this, movementSet, index)
        }
    }

    List<MovementSet> getCashIn(){
        return WorkoutMovementSet.findAllByWorkoutAndGroupIdentifier(this, "cashIn").sort { it.setIndex }.collect { it.movementSet } as List;
    }

    void setCashIn(List<MovementSet> sets){
        sets.eachWithIndex { movementSet, index ->
            WorkoutMovementSet.create(this, movementSet, index, "cashIn")
        }
    }

    List<MovementSet> getCashOut(){
        return WorkoutMovementSet.findAllByWorkoutAndGroupIdentifier(this, "cashOut").sort { it.setIndex }.collect { it.movementSet } as List;
    }

    void setCashOut(List<MovementSet> sets){
        sets.eachWithIndex { movementSet, index ->
            WorkoutMovementSet.create(this, movementSet, index, "cashOut")
        }
    }
}

还有链接表...

class WorkoutMovementSet implements Serializable{

    String id
    Workout workout
    MovementSet movementSet
    String groupIdentifier
    int setIndex

    static mapping = {
        id generator: 'uuid', params: [separator: '-']
        table 'workout_movementset'
        version false
    }

    static constraints = {
        groupIdentifier(nullable: true)
    }

    static WorkoutMovementSet create(Workout workout, MovementSet movementSet, int index, String groupId = null, boolean flush = false, failOnError = false) {
        WorkoutMovementSet instance = new WorkoutMovementSet(workout: workout, movementSet: movementSet, setIndex: index, groupIdentifier: groupId)
        instance.save(flush: flush, insert: true, failOnError: failOnError)
        return instance
    }

    static boolean remove(Workout workout, MovementSet movementSet, String groupId, boolean flush = false, failOnError = false) {
        WorkoutMovementSet instance = WorkoutMovementSet.findByWorkoutAndMovementSetAndGroupIdentifier(workout, movementSet, groupId)
        return instance ? instance.delete(flush: flush, failOnError: failOnError) : false
    }
}

从Grails 2.3.11升级到Grails 3.3.8后,这些关系仅在删除Workout中的设置器后才起作用。否则,休眠映射将在映射过程中引发以下异常:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'methodValidationPostProcessor' defined in class path resource [org/springframework/boot/autoconfigure/validation/ValidationAutoConfiguration.class]: Unsatisfied dependency expressed through method 'methodValidationPostProcessor' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateDatastoreServiceRegistry': Cannot resolve reference to bean 'hibernateDatastore' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateDatastore': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.grails.orm.hibernate.HibernateDatastore]: Constructor threw exception; nested exception is org.hibernate.MappingException: collection element mapping has wrong number of columns: fitnerds.coreservices.domain.Workout.cashOut type: object
        at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:467)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1181)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1075)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
        at org.springframework.context.support.PostProcessorRegistrationDelegate.registerBeanPostProcessors(PostProcessorRegistrationDelegate.java:225)
        at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:703)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:528)
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
        at grails.boot.GrailsApp.run(GrailsApp.groovy:84)
        at grails.boot.GrailsApp.run(GrailsApp.groovy:393)
        at grails.boot.GrailsApp.run(GrailsApp.groovy:380)
        at grails.boot.GrailsApp$run.call(Unknown Source)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:136)
        at tw.api.Application.main(Application.groovy:8)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateDatastoreServiceRegistry': Cannot resolve reference to bean 'hibernateDatastore' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateDatastore': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.grails.orm.hibernate.HibernateDatastore]: Constructor threw exception; nested exception is org.hibernate.MappingException: collection element mapping has wrong number of columns: fitnerds.coreservices.domain.Workout.cashOut type: object
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
        at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:648)
        at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:145)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1201)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1103)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getSingletonFactoryBeanForTypeCheck(AbstractAutowireCapableBeanFactory.java:931)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryBean(AbstractAutowireCapableBeanFactory.java:808)
        at org.springframework.beans.factory.support.AbstractBeanFactory.isTypeMatch(AbstractBeanFactory.java:564)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:432)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:395)
        at org.springframework.beans.factory.BeanFactoryUtils.beanNamesForTypeIncludingAncestors(BeanFactoryUtils.java:206)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1267)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1101)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
        at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835)
        at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
        ... 24 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateDatastore': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.grails.orm.hibernate.HibernateDatastore]: Constructor threw exception; nested exception is org.hibernate.MappingException: collection element mapping has wrong number of columns: fitnerds.coreservices.domain.Workout.cashOut type: object
        at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:279)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1201)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1103)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351)
        ... 40 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.grails.orm.hibernate.HibernateDatastore]: Constructor threw exception; nested exception is org.hibernate.MappingException: collection element mapping has wrong number of columns: fitnerds.coreservices.domain.Workout.cashOut type: object
        at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:154)
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:122)
        at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:271)
        ... 49 common frames omitted
    Caused by: org.hibernate.MappingException: collection element mapping has wrong number of columns: fitnerds.coreservices.domain.Workout.cashOut type: object
            at org.hibernate.mapping.Collection.validate(Collection.java:315)
            at org.hibernate.mapping.IndexedCollection.validate(IndexedCollection.java:74)
            at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:333)
            at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:464)
            at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:711)
            at org.grails.orm.hibernate.cfg.HibernateMappingContextConfiguration.buildSessionFactory(HibernateMappingContextConfiguration.java:276)
            at org.grails.orm.hibernate.connections.HibernateConnectionSourceFactory.create(HibernateConnectionSourceFactory.java:86)
            at org.grails.orm.hibernate.connections.AbstractHibernateConnectionSourceFactory.create(AbstractHibernateConnectionSourceFactory.java:39)
            at org.grails.orm.hibernate.connections.AbstractHibernateConnectionSourceFactory.create(AbstractHibernateConnectionSourceFactory.java:23)
            at org.grails.datastore.mapping.core.connections.AbstractConnectionSourceFactory.create(AbstractConnectionSourceFactory.java:64)
            at org.grails.datastore.mapping.core.connections.AbstractConnectionSourceFactory.create(AbstractConnectionSourceFactory.java:52)
            at org.grails.datastore.mapping.core.connections.ConnectionSourcesInitializer.create(ConnectionSourcesInitializer.groovy:24)
            at org.grails.orm.hibernate.HibernateDatastore.<init>(HibernateDatastore.java:201)
            at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
            at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
            at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
            at org.springsource.loaded.ri.ReflectiveInterceptor.jlrConstructorNewInstance(ReflectiveInterceptor.java:1076)
            at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142)
            ... 51 common frames omitted

我认为,重现此问题的步骤需要涉及在域对象中映射多个相同类型的集合,并为这些集合使用声明的类型安全设置器。现在,我已经删除了二传手,但是我们很乐意保留它们:)

也许有人可以启发我。这是由具有相同类型的多个集合关系引起的,还是由于链接表上的额外groupIdentifier属性而抱怨额外的列?或者是其他东西?

软件版本为Grails 3.3.8,Hibernate 5,Java 1.8.0_181

0 个答案:

没有答案