Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping hibernate components to a separate table

Tags:

java

hibernate

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.

like image 235
skaffman Avatar asked Sep 14 '09 09:09

skaffman


1 Answers

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.

like image 91
ChssPly76 Avatar answered Nov 08 '22 11:11

ChssPly76