I'm really having trouble creating some queries for my domain model. I need some guidance.
I have two classes Component, and Group that contain DocumentInstances through a OneToMany relationship. I'm using two join tables to map that relationship.
Here is the mapping for Group and Component:
Code:
public class Component {
@OneToMany(fetch = FetchType.LAZY, targetEntity = DocumentInstance.class )
@JoinTable(
name="DocumentInstances_Components",
joinColumns = { @JoinColumn( name="ComponentID") },
inverseJoinColumns = @JoinColumn( name="DocumentInstanceID")
)
private Set<DocumentInstance> documentInstances;
}
public class Group {
@OneToMany(fetch = FetchType.LAZY, targetEntity = DocumentInstance.class )
@JoinTable(
name="DocumentInstances_Groups",
joinColumns = { @JoinColumn( name="GroupID") },
inverseJoinColumns = @JoinColumn( name="DocumentInstanceID")
)
private Set<DocumentInstance> documentInstances;
}
Now I have DocumentInstance and it contains two associations to SchemaNamespace through a ManyToOne and BinaryData through a OneToOne relationship. Here is his mapping:
Code:
public class DocumentInstance {
@ManyToOne( fetch = FetchType.EAGER, cascade = CascadeType.ALL )
@JoinColumn( name = "NamespaceID", nullable = false )
private SchemaNamespace namespace;
@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn( name = "BinaryDataID" )
private BinaryData binaryData;
}
Now I'm trying to fetch the associated BinaryData object given a Group and SchemaNamespace object, and given a Component and SchemaNamespace object (i.e. two different queries). This seems like it's a polymorphic query, but I don't know how to specify the id for a group vs. a component. I read in the Manning book that this type of query isn't supported by Hibernate, but I could map it using the <any> element. I have no idea what this might be in annotations. This is where I begin to think that I might need to seperate tables to map this because of the query I'm trying to run. One for DocumentInstances belonging to Components and one for DocumentInstances belonging to Groups. If I did this how would I create two mappings for one object with annotations? Then what's worse using <any> elements or mapping an object twice?
Should I create an Interface for Group and Component to implement and map an owner property in DocumentInstance. If I do that how will Hibernate know I want the DocumentInstance with this namespace and owned by this Group vs. I want the DocumentInstance with this namespace and owned by this Component?
I tried a query like:
Code:
from Group as g join g.documentInstances as di where
g.id = :id
and di.namespace.namespace = :namespace
But that loaded a full group object with all of it's DocumentInstances fully loaded, and it gave me a DocumentInstance in a Object[] array. Seems like I need to do something like:
Quote:
from DocumentInstance di join Group g where g.id = :id and di.namespace.namespace = :namespace
This would seem like a quite common problem.
Utterly confused.
Charlie