I've been trying to figure out this mapping all day and I just can't get it right. I've been reading up on all kinds of similar problems but just can't seem to apply the same to my specific example.
Here's my model (this is a legacy model, which I cannot change):
One 'Type' can have many 'Codes'.
Type:#TYPE_ID
TYPE_CATEGORY
Code:#CODE_CATEGORY
#CODE_ID
I want a list of Codes attached to a Type - I need to look up all the codes in the Code table that have the same category.
After trying many different configurations, here's my mapping:
Code:
<bag name="categories" cascade="none" lazy="false">
<key foreign-key="TYPE_CATEGORY">
<column name="CODE_CATEGORY"/>
<column name="CODE_ID"/>
</key>
<one-to-many class="com....Code" />
</bag>
But my categories collection is always empty, even if data exists that should be found.
The complicating factors here seem to be:
- There's a missing link table, category should be an entity in it's own right, but it's existence is not explicit even though it is being used to link the entities Type and Code
- Code has a composite key
- The columns that are supposed to be matched (TYPE_CATEGORY and CODE_CATEGORY) are not named the same, and I can't find a way to correctly express the link of TYPE_CATEGORY and CODE_CATEGORY in the mapping.
Does anyone have any idea how this can be mapped? I know this is one of those poor designed, legacy schema situations, but it doesn't seem too uncommon and I've certainly seen hibernate deal with worse.