Hibernate version: 3.2.4sp1
I am attempting to use a CompositeUserType to define a polymorphic value type association as suggested by numerous posts. My value type is a bit unusual (although not considered exotic) in that it has a many valued association to another value type.
A -> C <<value type>> ->* E
In this diagram, entity A has a component C which in-turn has a many valued association with composite-element E. And left out of the diagram are the subtypes of C which I think are not directly relevant to the problem. Also I need to mention that A is also subclassed, therefore it would not be possible to merge C and A together without class explosion and code duplication. Normally this would be mapped using a <component> containing a <set> containing a <composite-element>, but as previously mentioned, I need a UserType. I realize that I could map C as an entity, but it would require an extra table containing nothing other than a one-to-one constrained id and a discriminator. The result is undesirable as we would have a non-optimal schema, and compromised object model on top of it.
I tried developing a CompositeUserType to map this and nesting a SetType property type. Notably missing are the column mappings, as i haven't quite figured out how to map these. I am getting a MappingException with the message "Unknown collection role" at runtime, after the application has already initialized, and only after loading/querying for A and flushing.
Some of the more interesting code is here with non-relevant members omitted for clarity:
Code:
public class CType implements CompositeUserType {
Type[] getPropertyTypes() {
return new Type[]{new SetType(A.class.getName() + ".c.es", null, true)}
}
}
public class C {
private Set<E> es;
}
The schema I would like to create looks like this: non-relevant attributes omitted for clarity.
Code:
a (id, a_discriminator, c_discriminator)
e (a_id)
After delving into the source code with a debugger, I have determined that the SessionFactory's collectionPeristers does not contain a role for the collection of Es within C. It gets these collectionPersisters from the Configuration object's collection mappings. My initial thoughts are that I need to somehow create a "Collection" or "Set" object to add to Mappings.collections, but it's not clear how this can be done as CompositeUserType does not have seem to have hooks to manipulate the Mappings object. My best guess is that I'll need to manipulate the Configuration object by manually adding a collection to the Mappings before building the session factory.
I'd like some advice as to what is the best way to create the mapping.