I have a class "A" that has a property of abstract class "B" in a one to one relationship. "A" will have only one "B", but the "B" will change from time to time to different subclasses. Any instantiated "B" subclass will only be related to one "A". "B" does not have a reference to "A".
Code:
public class A {
String id;
int version;
B myB;
}
public abstract class B {
String it; // all subclasses of B have the same properties, but different behavior
abstract void doIT();
}
public class B1 {
void doIT() {
// differs
}
}
Please tell me if I'm wrong in thinking that:
1. I cannot map "B" as a component of "A" because it is subclassed.
2. I cannot use a one-to-one with primary key associations without either:
2a. adding a property to "B" to know "A" (impossible in this situation),
or:
2b. having "A"'s id change every time it's "B" changes (very, very inconvenient because I need to refer to objects by their id).
3. I can use a many-to-many from "A" to "B"
I have successfully implemented #3 in my list, but have a second problem when I do. I cannot use cascade="all-delete-orphan" with a many-to-many relationship, so every instantiated "B" object is saved and never deleted. If an "A" goes through 12 different "B"s, all 12 remain in the database despite the object graph only needing one of them.
Does anyone have any suggestions about how I should map this so that I don't have many unnecessary rows in my "B" table?
Thanks for any suggestions,
Scott