Hibernate collection multiple types

Posted by CaptainAwesomePants on Stack Overflow See other posts from Stack Overflow or by CaptainAwesomePants
Published on 2010-04-14T21:26:22Z Indexed on 2010/04/14 21:43 UTC
Read the original article Hit count: 259

Filed under:

I have a class Player that contains a list of Accessory objects. There are two kinds of Accessories. SocketedAccessories have a list of SocketJewels, and MagicAccessories have a list of MagicEnchantments.

At the database level, there is a players table that represents the player, and an accessories table that contains a list of accessories. Accessories have a type field that indicates whether they are socketed or magical, and the columns that are only used by one type are just left blank by entries of the other type. There are socket_jewels and magic_enchantments tables, representing the socket jewels or the magic enchantments on each accessory.

I am trying to figure out the correct way to map this with Hibernate. One way would be for the player to have two lists of accessories, one for SocketedAccessories and one for MagicAccessories. That seems undesirable, though. What I want is a way to specify that player should have a field List<Accessory> accessories that contains both types of thing.

Is there a way to tell Hibernate, in either hbm.xml or annotations, to do this?

© Stack Overflow or respective owner

Related posts about hibernate