如何强制实施单向一对多关系?

时间:2014-09-03 08:47:53

标签: grails gorm one-to-many

我有以下结构

 class User {

     String fname
     String lname
     String email

     static constraints = {
       fname(nullable:true, maxSize:30)
       fname(nullable:true, maxSize:30)
       email(nullable: true, email: true)
     }
}

class A extends User{
    String smtng
    List bInstance = new ArrayList()

    static hasMany = [b : B] 

    static constraints = {
       smtng(blank : true)
       b(nullable : true)
    }
}

class B {
    String smtng1
    String smtng2

    static constraints = {
       smtng1(blank:true, maxSize:50)
       smtng2(blank:true, maxSize:50)
    }
}

当我尝试运行此代码时,它显示以下错误

完整堆栈跟踪........

    2014-09-03 12:37:11,010 [localhost-startStop-1] ERROR StackTrace  - Full Stack Trace:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pluginManager' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.codehaus.groovy.grails.exceptions.GrailsDomainException: Property [b] in class [class A] is a bidirectional one-to-many with two possible properties on the inverse side. Either name one of the properties on other side of the relationship [a] or use the 'mappedBy' static to define the property that the relationship is mapped with. Example: static mappedBy = [b:'myprop']
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1512)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:610)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:410)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
    at org.codehaus.groovy.grails.web.context.GrailsContextLoader.initWebApplicationContext(GrailsContextLoader.java:70)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4973)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5467)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
Caused by: org.codehaus.groovy.grails.exceptions.GrailsDomainException: Property [b] in class [class A] is a bidirectional one-to-many with two possible properties on the inverse side. Either name one of the properties on other side of the relationship [a] or use the 'mappedBy' static to define the property that the relationship is mapped with. Example: static mappedBy = [b:'myprop']
    at org.codehaus.groovy.grails.commons.DefaultGrailsDomainClass.establishRelationshipForCollection(DefaultGrailsDomainClass.java:362)
    at org.codehaus.groovy.grails.commons.DefaultGrailsDomainClass.establishRelationships(DefaultGrailsDomainClass.java:262)
    at org.codehaus.groovy.grails.commons.DefaultGrailsDomainClass.<init>(DefaultGrailsDomainClass.java:120)
    at org.codehaus.groovy.grails.commons.DomainClassArtefactHandler.newArtefactClass(DomainClassArtefactHandler.java:54)
    at org.codehaus.groovy.grails.commons.AnnotationDomainClassArtefactHandler.newArtefactClass(AnnotationDomainClassArtefactHandler.java:54)
    at org.codehaus.groovy.grails.commons.DefaultGrailsApplication.addArtefact(DefaultGrailsApplication.java:812)
    at org.codehaus.groovy.grails.commons.DefaultGrailsApplication.addArtefact(DefaultGrailsApplication.java:538)
    at org.codehaus.groovy.grails.commons.DefaultGrailsApplication.configureLoadedClasses(DefaultGrailsApplication.java:259)
    at org.codehaus.groovy.grails.commons.DefaultGrailsApplication.initialise(DefaultGrailsApplication.java:743)
    at org.codehaus.groovy.grails.plugins.GrailsPluginManagerFactoryBean.afterPropertiesSet(GrailsPluginManagerFactoryBean.java:123)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1571)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1509)
    ... 23 more
2014-09-03 12:37:11,015 [localhost-startStop-1] ERROR StackTrace  - Full Stack Trace:
org.codehaus.groovy.grails.exceptions.GrailsDomainException: Property [b] in class [class A] is a bidirectional one-to-many with two possible properties on the inverse side. Either name one of the properties on other side of the relationship [a] or use the 'mappedBy' static to define the property that the relationship is mapped with. Example: static mappedBy = [b:'myprop']
    at org.codehaus.groovy.grails.commons.DefaultGrailsDomainClass.establishRelationshipForCollection(DefaultGrailsDomainClass.java:362)
    at org.codehaus.groovy.grails.commons.DefaultGrailsDomainClass.establishRelationships(DefaultGrailsDomainClass.java:262)
    at org.codehaus.groovy.grails.commons.DefaultGrailsDomainClass.<init>(DefaultGrailsDomainClass.java:120)
    at org.codehaus.groovy.grails.commons.DomainClassArtefactHandler.newArtefactClass(DomainClassArtefactHandler.java:54)
    at org.codehaus.groovy.grails.commons.AnnotationDomainClassArtefactHandler.newArtefactClass(AnnotationDomainClassArtefactHandler.java:54)
    at org.codehaus.groovy.grails.commons.DefaultGrailsApplication.addArtefact(DefaultGrailsApplication.java:812)
    at org.codehaus.groovy.grails.commons.DefaultGrailsApplication.addArtefact(DefaultGrailsApplication.java:538)
    at org.codehaus.groovy.grails.commons.DefaultGrailsApplication.configureLoadedClasses(DefaultGrailsApplication.java:259)
    at org.codehaus.groovy.grails.commons.DefaultGrailsApplication.initialise(DefaultGrailsApplication.java:743)
    at org.codehaus.groovy.grails.plugins.GrailsPluginManagerFactoryBean.afterPropertiesSet(GrailsPluginManagerFactoryBean.java:123)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1571)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1509)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:610)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:410)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
    at org.codehaus.groovy.grails.web.context.GrailsContextLoader.initWebApplicationContext(GrailsContextLoader.java:70)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4973)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5467)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
2014-09-03 12:37:11,018 [localhost-startStop-1] ERROR StackTrace  - Full Stack Trace:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pluginManager' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.codehaus.groovy.grails.exceptions.GrailsDomainException: Property [b] in class [class A] is a bidirectional one-to-many with two possible properties on the inverse side. Either name one of the properties on other side of the relationship [a] or use the 'mappedBy' static to define the property that the relationship is mapped with. Example: static mappedBy = [b:'myprop']
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
Caused by: org.codehaus.groovy.grails.exceptions.GrailsDomainException: Property [b] in class [class A] is a bidirectional one-to-many with two possible properties on the inverse side. Either name one of the properties on other side of the relationship [a] or use the 'mappedBy' static to define the property that the relationship is mapped with. Example: static mappedBy = [b:'myprop']
    ... 5 more
2014-09-03 12:37:11,020 [localhost-startStop-1] ERROR StackTrace  - Full Stack Trace:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pluginManager' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.codehaus.groovy.grails.exceptions.GrailsDomainException: Property [b] in class [class A] is a bidirectional one-to-many with two possible properties on the inverse side. Either name one of the properties on other side of the relationship [a] or use the 'mappedBy' static to define the property that the relationship is mapped with. Example: static mappedBy = [b:'myprop']
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
Caused by: org.codehaus.groovy.grails.exceptions.GrailsDomainException: Property [b] in class [class A] is a bidirectional one-to-many with two possible properties on the inverse side. Either name one of the properties on other side of the relationship [a] or use the 'mappedBy' static to define the property that the relationship is mapped with. Example: static mappedBy = [b:'myprop']
    ... 5 more
2014-09-03 12:37:11,022 [localhost-startStop-1] ERROR StackTrace  - Full Stack Trace:
org.codehaus.groovy.grails.exceptions.GrailsDomainException: Property [b] in class [class A] is a bidirectional one-to-many with two possible properties on the inverse side. Either name one of the properties on other side of the relationship [a] or use the 'mappedBy' static to define the property that the relationship is mapped with. Example: static mappedBy = [b:'myprop']
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
2014-09-03 12:37:11,023 [localhost-startStop-1] ERROR StackTrace  - Full Stack Trace:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pluginManager' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.codehaus.groovy.grails.exceptions.GrailsDomainException: Property [b] in class [class A] is a bidirectional one-to-many with two possible properties on the inverse side. Either name one of the properties on other side of the relationship [a] or use the 'mappedBy' static to define the property that the relationship is mapped with. Example: static mappedBy = [b:'myprop']
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
Caused by: org.codehaus.groovy.grails.exceptions.GrailsDomainException: Property [b] in class [class A] is a bidirectional one-to-many with two possible properties on the inverse side. Either name one of the properties on other side of the relationship [a] or use the 'mappedBy' static to define the property that the relationship is mapped with. Example: static mappedBy = [b:'myprop']
    ... 5 more
2014-09-03 12:37:11,024 [localhost-startStop-1] ERROR StackTrace  - Full Stack Trace:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pluginManager' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.codehaus.groovy.grails.exceptions.GrailsDomainException: Property [b] in class [class A] is a bidirectional one-to-many with two possible properties on the inverse side. Either name one of the properties on other side of the relationship [a] or use the 'mappedBy' static to define the property that the relationship is mapped with. Example: static mappedBy = [b:'myprop']
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
Caused by: org.codehaus.groovy.grails.exceptions.GrailsDomainException: Property [b] in class [class A] is a bidirectional one-to-many with two possible properties on the inverse side. Either name one of the properties on other side of the relationship [a] or use the 'mappedBy' static to define the property that the relationship is mapped with. Example: static mappedBy = [b:'myprop']
    ... 5 more
2014-09-03 12:37:11,026 [localhost-startStop-1] ERROR StackTrace  - Full Stack Trace:
org.codehaus.groovy.grails.exceptions.GrailsDomainException: Property [b] in class [class A] is a bidirectional one-to-many with two possible properties on the inverse side. Either name one of the properties on other side of the relationship [a] or use the 'mappedBy' static to define the property that the relationship is mapped with. Example: static mappedBy = [b:'myprop']
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
2014-09-03 12:37:11,027 [localhost-startStop-1] ERROR StackTrace  - Full Stack Trace:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pluginManager' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.codehaus.groovy.grails.exceptions.GrailsDomainException: Property [b] in class [class A] is a bidirectional one-to-many with two possible properties on the inverse side. Either name one of the properties on other side of the relationship [a] or use the 'mappedBy' static to define the property that the relationship is mapped with. Example: static mappedBy = [b:'myprop']
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
Caused by: org.codehaus.groovy.grails.exceptions.GrailsDomainException: Property [b] in class [class A] is a bidirectional one-to-many with two possible properties on the inverse side. Either name one of the properties on other side of the relationship [a] or use the 'mappedBy' static to define the property that the relationship is mapped with. Example: static mappedBy = [b:'myprop']
    ... 5 more

注意: - 我想要A类和B类之间的单向一对多关系 我需要做些什么来创建这些单向关系? 感谢

3 个答案:

答案 0 :(得分:1)

您的具体示例毫无意义,因为B具有类型A no 属性,这些属性可能是后向引用的候选对象,更不用说两种可能的替代方案。但一般来说,如果您想确保hasMany关系是单向的,则可以使用mappedBy "none"的值:

class A extends User{
    String smtng
    List b // note: do not include an initializer expression here

    static hasMany = [b : B]

    static mappedBy = [b:"none"]

    static constraints = {
       smtng(blank : true)
       b(nullable : true)
    }
}

答案 1 :(得分:0)

class A extends User{
   String smtng
   List bInstance

   static hasMany = [bInstance : B] 

   static constraints = {
      bInstance(nullable : true)
   }
}

class B {
  String smtng1
  String smtng2

  static belongsTo = [A]
}

答案 2 :(得分:0)

执行异常所说的内容:

class A extends user{
  static hasMany = [bInstance : B] 
  static mappedBy = [ B:'bref' ]
}

更新

或明确定义back-ref:

class B {
  static belongsTo = [ a:A ]
}