grails - 如何执行单向一对多关系?

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

最佳答案

您的特定示例没有任何意义,因为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)
    }
}

关于grails - 如何执行单向一对多关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25640016/

相关文章:

grails - 没有此类属性:类的org.grails.orm.hibernate.cfg.HibernatePersistentEntity

grails - 如何使用GORM测试实例是否在数据库中持久存在?

java - JPA 一对多在映射后给出空值

grails - Gorm一对一和一对多

grails - Groovy 列表根据两个标准排序

grails - Grails GORM系列

grails - Grails GORM MongoDB插件中是否有类似可搜索的search()方法的类似方法?

c# - Ado.Net 实体 : Object doesn't display linked members (foreign keys)

grails - 一对多和一对一 grails 2.2.4

tomcat - Tomcat 上的 Grails - 如何记录原始 HTTP 请求/响应