I currently have a modelling problem , where I don't know how to model a simple composite-pattern. I have these classes:
A is abstract
B, C, D, E extend A
I model the inheritance using
@Inheritance(strategy=InheritanceType.JOINED)
B cannot be contained in other classes(isRoot)
B has a collection of sub-units, that can contain C and D
C can only contain classes of type E
E can contain no more other classes (isLeaf)
My first was to model this using a UnitType and a Jointable for AllowedSubunits:
UnitType
boolean isRoot
boolean isLeaf
Collection<UnitType> allowedSubtypes
AllowedSubunit:
parentUnit_id
subunit_id
I then add a many-to-one to the
----
This works quite ok, but has some drawbacks:
- I introduce a second typesystem (the A,B,C,D,E AND the UnitTypes) - there is nothing but the code that keeps this together.
- I have to hardcode the unitTypes in the constructors of the classes, to be able to set the correct unittype before I .save(newUnit)
----
Does anyone have a cleverer (and hopefully simpler) idea how to model this?!
cheers
stf
|