Reputation: 59
Hi all im having trouble extending entities and persisting them to the data base, my model is has folows,
People - Base entities, which has to be persisted
Friend - leaf entities
Coworker - leaf entities
the problem arise when i get a person from the data base and then i want to "Make it" a friend, instead is creating another People row with the same values, and i want to take the existing one.
Im using join strategy has my inheritance strategy.
any clues ??
Upvotes: 0
Views: 416
Reputation: 40036
I bet what do you mean by "leaf entity" is "Friend and Coworker inherit People", right?
What you are trying to achieve is not suitable to use inheritance.
Just think in a plain OO way:
You have instantiated a "Person" (as suggested by other comment, it is more reasonable to use Person instead of People), you cannot make that instance become a "Friend".
You should consider making Friend / Coworker separate entities, and treat them as extra attributes that a Person carries. e.g.
@Entity
class Person {
@OneToOne(mappedBy="people")
FriendAttribute friendAttribute;
@OneToOne(mappedBy="people")
CoworkerAttribute coworkerAttribute;
}
@Entity
class FriendAttribute {
@OneToOne
Person person;
// other friend-related attributes
}
@Entity
class CoworkerAttribute {
@OneToOne
Person person;
// other coworker-related attributes
}
By such way, if you want to make a Person being a Coworker, you are going to assign Coworker Attribute to that Person.
Honestly this question have nothing to do with Hibernate or JPA. It simply doesn't work in plain POJO too, as what I suggested before. In Java, you simply cannot make an instance of a class become instance of another class.
Upvotes: 2