Hello.
I have problems creating new instance of joined subclass from already existing object of superclass.
The classes in question are:
Code:
@Entity
@Table(name = "person_v")
@SequenceGenerator(name = "SEQ_STORE", sequenceName = "person_id_seq")
@Inheritance(strategy = InheritanceType.JOINED)
public class Person
{
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_STORE")
@Column(name = "person_id", nullable = false)
long id;
....
}
@Entity
@Table (name = "DEBTOR")
@PrimaryKeyJoinColumn(name = "DEBTOR_ID", referencedColumnName = "PERSON_ID")
public class Debtor extends Person {
... some additional fields ...
}
So, Debtor extends Person and they both share primary key value. I can create entirely new Debtor without problems and have it stored in database.
Problem arises, when i already have a Person and i want to make Debtor from it without changing Person data (ie. just insert appropriate data into Debtor table). If i make new Debtor object and just copy the fields, i get
Code:
org.hibernate.WrongClassException: Object with id: null was not of the specified subclass: model.Debtor (class of the given object did not match class of persistent copy)
Any idea if such situation can be solved at all? I've looked through unit-test code, documentation and examples and i haven't seen similar problem solved anywhere. I've thought of manually inserting debtor_id into database, then load Debtor object by id, fill in the rest of the fields and persist it but there has to be a better way for doing this.