Hi,
I am not sure if it is an imperfection of my architecture or it is some other problem.
I have a class Person:
Code:
@Entity
@Table(name="persons")
@Inheritance(strategy=InheritanceType.JOINED)
public abstract class Person extends BaseObject { ... }
I have another class Parent:
Code:
@Entity
@Table(name="parents")
public class Parent extends Person { ... }
And finally I have a class Fiancee:
Code:
@Entity
@Table(name="fiancees")
public class Fiancee extends Person {
public Fiancee(Person person) {
this.setId(person.getId()); //???
...
}
}
As you can see I am using "Joined subclasses" inheritance strategy.
Of course object of class Parent is not an object of class Fiancee and vice-versa. When I am creating an object of class Parent and saving it - there is a new record in my persons and parents tables.
But what if I would like my Person to be a Parent and a Fiancee? Imagine the situation: I have a Parent 'Mary'. This Person is a Parent in one form but in the other form it should be a Fiancee. So I would like to have in my database a record in persons, parents and fiancees tables with a common joining key.
Unfortunately when I load an existing Parent object and make a Fiancee object from it:
Code:
new Fiancee(parent)
and try to save this object - I get "Key (fiancee_id)=(1) does not exist in "fiancees" table".
I understand that. But when I remove the line "this.setId(person.getId());" from the Fiancee constructor I can save the object but a new Person is created in the database as well as a new Fiancee - and I do not want the new Person object created.
Could someone give me some pointers? I have read the Hibernate docs about inheritance few times but I still can not find the answer...
Best Regards,
Gandalf