I have this situation:
I have an entity, Person
, that contains all personal details of a person, like birth date, street address, municipality ecc ecc.
And I have an entity ClubMember
that describe a member of a club and contains some field like: registration date, type of member, credit, ecc ecc
So, a ClubMember
is a Person
, and I think is correct describe this with a inheritance:
ClubMember extends Person
, but, what type of strategy?
I would obtain two table in database, Person
and ClubMember
, with ClubMember
that contain id_person
like an @OneToOne
relationship, but keep the inheritance between entities; is this possible (and is correct)?
JOINED
is the strategy closest to my target, but I lose the ID
field of table ClubMember
, in fact ID
become the ID
of table Person
…
Answer
Blow, you should keep performance issues when choosing some kind of inheritance strategy. Here you can see a good insight about available inheritance strategies. Do not forget a inheritance can be re-writen as a association as follows
instead of
public class A {} public class B extends A {}
you can use
public class B { private A a; }
Or you can use MapedSuperClass To map all of your inherited properties whitout using any Hibernate/JPA inheritance strategy. See here
Keep in mind when using default JPA/Hibernate annotations, Hibernate always fetch all of subclasses. If you have a single hierarchy, prefer To use Single Table Inheritance strategy. Joined strategy is better designed when you have a complex hierarchy but it suffers due To performance issues when querying for any entity.