2014-09-03 3 views
1

I имеют следующую структуруКак применять однонаправленные отношения «один ко многим»?

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) 
    } 
} 

, когда я пытаюсь запустить этот код он показывает следующую ошибку

Full Stack след ........

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. Что мне нужно сделать для создания этих однонаправленных отношений? Благодаря

+0

'' A' расширяет User'. Как выглядит ваш «Пользователь»? – saw303

+0

i Обновлен вопрос ... ничего особенного в пользовательском классе. – abdul

+0

Пожалуйста, добавьте содержимое своих ограничений. Я не могу воспроизвести вашу проблему на простом новом приложении Grails. – saw303

ответ

1

Ваш конкретный пример не имеет смысла, так как 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) 
    } 
} 
+1

извините за пример. Его просто образец – abdul

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] 
} 
+0

Я уже это пробовал. все еще показывая ту же ошибку – abdul

0

делать то, что говорит исключение:

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

UPDATE

или определить обратную реф явно:

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

Я все же показывал ту же ошибку – abdul

+0

'mappedBy' кажется не обязательным для однонаправленных отношений. http://grails.org/doc/latest/ref/Domain%20Classes/mappedBy.html – saw303

+0

нет, конечно нет – injecteer

Смежные вопросы