I am using Hibernate Annotations 3.2.1 and Hibernate 3.2.4 from Jboss Seam 2.0.0.
I have four entity classes, lets call them A, AA, B and BB with the following relationships:
- AA extends A
- BB extends B
- A has a one to many relationship with B.
Now I want to add a restriction: If the entity class is A, it should have one to many relationship only with entities of class B; conversely, If the entity class is AA, it should have one to many relationship only with entities of class BB.
My first approximation was to create two generic mapped superclasses A0 and B0 and use them as a base this way:
Code:
@MapperSuperclass
class A0<X> {
...
@OneToMany
Set<X> getChildren() {
...
}
@MappedSuperclass
class B0<X> {
...
@ManyToOne
X getParent() {
...
}
@Entity
class A extends A0<B> {
...
}
@Entity
Class B extends B0<A> {
...
}
@Entity
class AA extends A0<BB> {
...
}
@Entity
Class BB extends B0<AA> {
...
}
but I am not happy with result because I see this as too convoluted and I lose the inheritance between (A and AA) and (B and BB).
How can I implement this structure?