Hi, everybody.
I am using hibernate as a persistence layer. There are 2 entities that live in the same table extending one superclass with single table inheritance strategy.
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class A {
@Id
@GeneratedValue
protected Long id;
// some common fields for B and C
}
@Entity
public class B extends A {
// B-specific fields
}
@Entity
public class C extends A {
// C-specific fields
}
I have an instance of B with id=4. How do I change the type of this instance to C preserving it's ID (4)?
B b = em.find(B.class, 4L);
C c = convertToC(b);
c.setId(b.getId());
em.remove(b);
em.persist(c);
The code above fails with
org.hibernate.PersistentObjectException: detached entity passed to persist: C
Is it possible at all?