Hey,
I'm currently having a Problem (or maybe just a understanding mistake). However, I'm more or less a newby in relation to the JPA.
The Topic is a "@OneToMany"-Relation to a abstract class.
Here is a short example what I basically want to do:
Code:
@Entity
class MyMaster {
@OneToMany
List<MyAbstract> setOfAbstracts;
}
@MappedSuperclass
abstract MyAbstract { ... }
@Entity
class MySlave1 extends MyAbstract { ... }
@Entity
class MySlave1 extends MyAbstract { ... }
Well, I (more or less) understand what is the problem with this solution. "MyAbstract" is no entity and therefore I could not genereate a "@OneToMany"-relation.
But what would be the correct way to do this (and how could I tell the code to do this)?
Here are some more detailed information about my project:
- Basically, it is a object oriented programming project at a university. Therefore, the structure of the database is not as important as the structure of the objects. The current structure of the objects is the best one (from OOP-point-of-view). So I don't want to change anything in the object-structure, even if the database is getting quite big and and unhandy.
- One "MySlave"-Object can only refer to one "MyMaster" (I don't have to block other relations, but maybe it's an helpfull information)
- Obviously, One "MyMaster" relates to many "MyAbstracts"
- In my project there are more than two "MySlaves"
I thought about a few solution, but don't know how the realize theme or they are not suitable for my application.
Solution 1: Two list in "MyMaster" to seperate the "MySlaves":
This is no solution, because I can't change the structure of the list in "MyMaster"
Solution 2 (from database point of view): A FK in the "MySlaves" to "MyMaster"
Basically, this would be a @ManyToOne from "MyAbstract" to "MyMaster". But how can I than (automatically) create the list of "MyAbstract" in "MyMaster"?
Solution 3 (from database point of view): A sperate entity "MyAbstract":
In the database this would be a seperate table for the fields of "MyAbstract" which points to "MySlave1" or "MySlave2" with the additional fields of them. Then I could relay have a @OneToMany in "MyMaster". However, I have no idea how I could tell the code to do this.
Can anyone please help me with a hint or a solution?
Thanks!
fhv_fb