我有以下结构

 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类之间的单向一对多关系
创建那些单向关系我该怎么做?
谢谢

最佳答案

您的特定示例没有任何意义,因为B没有A类型的属性可以作为反向引用的候选对象,更不用说两个可能的选择了。但是通常,如果要确保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)
    }
}

09-04 19:53