将hibernate组件映射到单独的表 [英] Mapping hibernate components to a separate table

查看:68
本文介绍了将hibernate组件映射到单独的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



可以配置Hibernate以将组件类存储在单独的表中吗?

 < class name =test.ClassA> 
< property name =propA/>
< component name =componentPropclass =test.ClassB>
< property name =propB/>
< / component>
< / class>

它映射到一个名为 MyClass 的表,并与两列 propA propB 。我想要的是将组件的属性映射到一个名为 ClassB 的表中。



不要要配置 ClassB 作为一个实体本身(它在 ClassA ),这样就排除了一个正常的关联。另外,我无法修改对象模型(它是生成的代码),因此我无法将ID属性引入 ClassB


$ b $这似乎是Hibernate功能上的一个缺陷 - < component>映射执行multiple-classes-to-one-table,< join>执行one-class-to-multiple-tables ,但奇怪的是没有明显的方式来做多班级到多个表,而不诉诸实体关联。



我想要这个的理由是我希望我的数据库模式尽可能地类似于对象模型,并且包含 ClassB 组件的单独表。我知道这不会扩展 - 例如,你不能做嵌套组件,但是在这种特殊情况下这不是问题。

解决方案 can 可以使用< join> < component>
 < class name =test.ClassA> 
< property name =propA/>

< join table =ClassB>
< key column =ClassA_id/>
< component name =componentPropclass =test.ClassB>
< property name =propB/>
< / component>
< / join>

< / class>

虽然您(显然)确实需要一个外键,但它不必映射到对象模型中。有关加入的详情,请参阅此处 - 仅为完整性提供,我知道知道从哪里获取它们: - )



上述链接的文档没有明确说任何关于映射连接内的组件,但DTD确实允许它,我已经在3.1中工作,所以我很确定它仍然可以正常工作。不过,不知道如何(或者是否有可能)使用注释来映射它。


Is it possible to configure Hibernate to store a component class in a separate table?

Take the following example:

<class name="test.ClassA">
   <property name="propA"/>
   <component name="componentProp" class="test.ClassB">
      <property name="propB"/>
   </component>
</class>

This maps to a table called MyClass with two columns propA and propB. What I want is to map the properties of the component to a table called ClassB.

What I don't want to do is configure ClassB as an entity in itself (it has no meaningful identity outside of ClassA), so that rules out a normal association. Also, I cannot modify the object model (it's generated code), so I can't introduce an ID property to ClassB.

This seems to be a gap in Hibernate's functionality - the <component> mapping performs "multiple-classes-to-one-table", and <join> does "one-class-to-multiple-tables", but oddly there's no apparent way of doing "multiple-classes-to-multiple-tables", without resorting to entity associations.

My rationale for wanting this is that I want my DB schema to resemble the object model as closely as is practical, and that includes separate tables for the ClassB component. I understand that this wouldn't scale - you couldn't do nested components, for example, but this isn't a problem in this particular situation.

解决方案

You can use <join> and <component> together, or did I misunderstand your question?

<class name="test.ClassA">
  <property name="propA"/>

  <join table="ClassB">
    <key column="ClassA_id" />
    <component name="componentProp" class="test.ClassB">
      <property name="propB"/>
    </component>
  </join>

</class>

While you (obviously) do need a foreign key, it does not have to be mapped in object model. Details on join are here - provided for completeness only, I know you know where to get them from :-)

Documentation at the above link does not explicitly say anything about mapping components within joins, but DTD does allow it and I've had it working in 3.1, so I'm pretty sure it still works fine. Don't know how (or whether it's possible) to map this with annotations, though.

这篇关于将hibernate组件映射到单独的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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