I have to maintain some JPA/Hibernate code that is used to manage some database tables. I have a class hierarchy like this:
@Entity
@Table(name="table_name")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="column_name", discriminatorType=DiscriminatorType.STRING)
public abstract class AbstractBaseClass {...}
@Entity
@DiscriminatorValue("N")
public class SubClass1 extends AbstractBaseClass { ...}
@Entity
@DiscriminatorValue("Y")
public class SubClass2 extends AbstractBaseClass { ...}
This is all working fine, but the database is being redesigned, the discriminatory column will be removed, and the type of the records will be determined by an SQL query. This redesign was not my idea, and I have no control over the database structure. It is also not practical to change the Java hierarchy, because of the existing code base. What can I use instead of DiscriminatorColumn/DiscriminatorValue so that everything continues to work?
If the discriminator can be determined using a simple SQL formula, you can use the hibernate-specific @DiscriminatorFormula extension:
Don’t know why they didn’t include this in the JPA standard :-(.