gpt4 book ai didi

在 Grails 中继承一个具体的非域类的 Hibernate MappingException

转载 作者:行者123 更新时间:2023-12-02 13:58:47 35 4
gpt4 key购买 nike

情景

我需要在两个不同的上下文中表示一个对象。一个上下文不应该持续存在,另一个应该。持久对象是从另一个系统中提取的实际数据。非持久对象代表产品定义的一部分。两者将进行比较,我对存储定义数据不感兴趣。持久化对象需要存储附加信息。

实现

为了实现这一点,我决定最合乎逻辑的做法是在 src/groovy 文件夹中创建基类,以避免 grails/hibernate 想要将其作为域类持久化。

class Resource{
String name
Date lastModified
}

我要保留的域类如下所示。
class OwnedResource extends Resource{  
Date dateCreated, lastUpdated
Owner owner
static belongsTo = [owner: Owner]
static mapping = {
//I assumed I would need this so that grails would not expect
//to store the object in the base class table that doesn't exist
tablePerHierarchy true
table 'owned_resource'
}
}

最后,我们拥有拥有许多拥有资源的 Owner 类。
class Owner{
Date dateCreated, lastUpdated
String name
static hasMany = [
resources: OwnedResource
]
}

问题

当我运行应用程序时,我最终得到一个友好的 Hibernate 异常:
2011-10-24 20:32:01,307 [main] ERROR context.GrailsContextLoader  - Error executing bootstraps: 
Error creating bean with name 'messageSource': Initialization of bean failed; nested exception
is org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean
property 'sessionFactory'; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'sessionFactory': Invocation of init method failed; nested exception is
org.hibernate.MappingException: Association references unmapped class: OwnedResource

org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'messageSource': Initialization of bean failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean
property 'sessionFactory'; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'sessionFactory': Invocation of init method failed; nested exception is
org.hibernate.MappingException: Association references unmapped class: OwnedResource
at org.grails.tomcat.TomcatServer.start(TomcatServer.groovy:212)
at grails.web.container.EmbeddableServer$start.call(Unknown Source)
at _GrailsRun_groovy$_run_closure5_closure12.doCall(_GrailsRun_groovy:158)
at _GrailsRun_groovy$_run_closure5_closure12.doCall(_GrailsRun_groovy)
at _GrailsSettings_groovy$_run_closure10.doCall(_GrailsSettings_groovy:280)
at _GrailsSettings_groovy$_run_closure10.call(_GrailsSettings_groovy)
at _GrailsRun_groovy$_run_closure5.doCall(_GrailsRun_groovy:149)
at _GrailsRun_groovy$_run_closure5.call(_GrailsRun_groovy)
at _GrailsRun_groovy.runInline(_GrailsRun_groovy:116)
at _GrailsRun_groovy.this$4$runInline(_GrailsRun_groovy)
at _GrailsRun_groovy$_run_closure1.doCall(_GrailsRun_groovy:59)
at RunApp$_run_closure1.doCall(RunApp.groovy:33)
at gant.Gant$_dispatch_closure5.doCall(Gant.groovy:381)
at gant.Gant$_dispatch_closure7.doCall(Gant.groovy:415)
at gant.Gant$_dispatch_closure7.doCall(Gant.groovy)
at gant.Gant.withBuildListeners(Gant.groovy:427)
at gant.Gant.this$2$withBuildListeners(Gant.groovy)
at gant.Gant$this$2$withBuildListeners.callCurrent(Unknown Source)
at gant.Gant.dispatch(Gant.groovy:415)
at gant.Gant.this$2$dispatch(Gant.groovy)
at gant.Gant.invokeMethod(Gant.groovy)
at gant.Gant.executeTargets(Gant.groovy:590)
at gant.Gant.executeTargets(Gant.groovy:589)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.hibernate.MappingException: Association references unmapped class: OwnedResource
... 24 more

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.hibernate.MappingException: Association references unmapped class: OwnedResource
... 24 more

Caused by: org.hibernate.MappingException: Association references unmapped class: OwnedResource
... 24 more

也许我的实现是不好的做法,因为在对 StackOverflow 进行了大量的谷歌搜索和搜索之后,我还没有遇到任何人面临同样的问题或尝试类似的实现。大多数人都在尝试使用抽象类,这确实有效。我希望类 Resource 是具体的,因为我需要实例化它。答案可能只是 grails 不允许这个功能,但我想听到一个明确的答案和任何可能的变通办法。我倾向于不得不复制类而不是使用继承。

我做错了什么,为什么会发生这个错误?这个实现可以在 grails 中完成吗?

最佳答案

在这种情况下,如果您尝试扩展域文件夹之外的类,则应将该类声明为抽象类。

否则 Hibernate 会尝试映射它,因为它不在域文件夹中,所以一切最终都会崩溃。

关于在 Grails 中继承一个具体的非域类的 Hibernate MappingException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7883707/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com