Hibernate MappingException在Grails中继承具体的非域类 [英] Hibernate MappingException with inheritance of a concrete non-domain class in Grails

查看:109
本文介绍了Hibernate MappingException在Grails中继承具体的非域类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景



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

执行



为了做到这一点,我决定最合理的做法是在src / groovy文件夹中创建基类以避免希望将grails / hibernate作为域类持久化。

  class资源{
字符串名称
日期lastModified
}
code>

我想保留的域类如下所示。

  class OwnedResource extends Resource {
Date dateCreated,lastUpdated
Owner owner
static belongsTo = [owner:Owner]
static mapping = {
//我假设我需要这个,这样grails不会希望
//将对象存储在不存在的基类表中
tablePerHierarchy true
table'owned_resource '
}
}

最后,我们拥有Owner类,它拥有许多OwnedResources 。

  class所有者{
日期dateCreated,lastUpdated
字符串名称
static hasMany = [
资源:OwnedResource
]
}



h3>

当我运行应用程序时,我最终变得友好Hibernate异常:

  c> 2011-10-24 20:32:01,307 [main] ERROR context.GrailsContextLoader  - 执行bootstraps时出错:
创建名为'messageSource'的bean时出错:bean初始化失败;嵌套异常
是org.springframework.beans.factory.BeanCreationException:创建名为
'transactionManager'的bean时出错:无法在设置bean时解析对bean'sessionFactory'的引用
property'sessionFactory';嵌套异常是
org.springframework.beans.factory.BeanCreationException:创建名为
'sessionFactory'的bean时出错:init方法的调用失败;嵌套的异常是
org.hibernate.MappingException:关联引用未映射的类:OwnedResource

org.springframework.beans.factory.BeanCreationException:创建名为
'messageSource'的bean时出错:初始化bean失败;嵌套异常是
org.springframework.beans.factory.BeanCreationException:创建名为
'transactionManager'的bean时出错:无法在设置bean时解析对bean'sessionFactory'的引用
property'sessionFactory';嵌套异常是
org.springframework.beans.factory.BeanCreationException:创建名为
'sessionFactory'的bean时出错:init方法的调用失败;嵌套的异常是
org.hibernate.MappingException:关联引用未映射的类:OwnedResource
在org.grails.tomcat.TomcatServer.start(TomcatServer.groovy:212)
在grails.web.container .EmbeddableServer $ start.call(Unknown Source)$ _ b在_GrailsRun_groovy $ _run_closure5_closure12.doCall(_GrailsRun_groovy:158)
在_GrailsRun_groovy $ _run_closure5_closure12.doCall(_GrailsRun_groovy)
在_GrailsS​​ettings_groovy $ _run_closure10.doCall(_GrailsS​​ettings_groovy :280)
。在_GrailsS​​ettings_groovy $ _run_closure10.call(_GrailsS​​ettings_groovy)$ b $在_GrailsRun_groovy $ _run_closure5.doCall(_GrailsRun_groovy b:149)
。在_GrailsRun_groovy $ _run_closure5.call(_GrailsRun_groovy)$ b $在_GrailsRun_groovy b .runInline(_GrailsRun_groovy:116)$ _ $ b at _GrailsRun_groovy.this $ 4 $ RunInline(_GrailsRun_groovy)
at _GrailsRun_groovy $ _run_closure1.doCall(_GrailsRun_groovy:59)
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 )

导致:org.springframework.beans.factory.BeanCreationException:创建名为'transactionManager'的bean时出错:无法在设置bean属性'sessionFactory'时解析对bean'sessionFactory'的引用。嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'sessionFactory'的bean时出错:init方法的调用失败;嵌套异常是org.hibernate.MappingException:关联引用未映射的类:OwnedResource
... 24 more

导致:org.springframework.beans.factory.BeanCreationException:创建名称为bean的错误'sessionFactory':调用init方法失败;嵌套异常是org.hibernate.MappingException:关联引用未映射类:OwnedResource
... 24 more

引起:org.hibernate.MappingException:关联引用未映射类:OwnedResource
... 24 more

也许我的实现是不好的做法,因为经过大量的搜索和搜索StackOverflow ,我还没有遇到任何人面临同样的问题或尝试类似的实施。大多数人都试图使用抽象类,这是行得通的。我希望类Resource是具体的,因为我需要实例化它。答案可能只是Grails不允许这个功能,但我想听到一个明确的答案和任何可能的解决办法。我倾向于不必使用继承来复制类。



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

解决方案

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



否则Hibernate会尝试映射它,因为它不在域文件夹中,所有东西最终都会炸毁。


The Scenario

I have a need to represent an object in two different contexts. One context should not persist and the other should. The persistent objects are actual data pulled from another system. The non-persistent objects represent parts of a product definition. The two will be compared, and I'm not interested in storing the definition data. The persisting object needs to have additional information stored with it.

The Implementation

In order to accomplish this, I decided that the most logical thing to do would be to create the base class in the src/groovy folder to avoid grails/hibernate wanting to persist it as a domain class.

class Resource{
    String name
    Date lastModified 
}

The domain class that I want to persist looks like the following.

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

Lastly we then have the Owner class which hasMany OwnedResources.

class Owner{
  Date dateCreated, lastUpdated
  String name
  static hasMany = [
    resources: OwnedResource
  ]
}

The Problem

When I run-app, I end up getting a friendly Hibernate exception:

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

Perhaps my implementation is bad practice, since after much googling and searching of StackOverflow, I have yet to encounter anyone facing this same issue or attempting a similar implementation. Most people are attempting to use abstract classes, which does work. I want the class Resource to be concrete because I need to instantiate it. The answer may simply be that grails doesn't allow this functionality, but I'd like to hear a definitive answer and any possible work arounds. I'm leaning toward having to duplicate the class instead of using inheritance.

What am I doing wrong and why is this error occurring? Can this implementation be done in grails?

解决方案

In this case if you are trying to extending a class that it is outside the domain folder, you should declare that class as abstract.

Otherwise Hibernate will try to map it, and because it´s not in the domain folder everything will eventually blow up.

这篇关于Hibernate MappingException在Grails中继承具体的非域类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆