情景
我需要在两种不同的上下文中表示一个对象。一个上下文不应该持续存在,而另一个应该。持久对象是从另一个系统中提取的实际数据。非持久对象代表产品定义的一部分。将两者进行比较,我对存储定义数据不感兴趣。持久化对象需要存储额外的信息。
实施
为了实现这一点,我决定最合乎逻辑的做法是在 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'
}
}
最后,我们拥有拥有许多 OwnedResources 的 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 中完成吗?