Hibernate discriminator column with table per subclass

Right now I am using a table per subclass approach to model my data. A simplification of my hierarchy is:

abstract class Abstract {
    /* common data stored in abstract */
}

class ConcreteTypeA1 extends Abstract {
    /* extra data stored in concrete_type_a_1 */
}

class ConcreteTypeA2 extends Abstract {
    /* extra data stored in concrete_type_a_2 */
}

class ConcreteTypeB extends Abstract {
    /* extra data stored in concrete_type_b */
}

So it does three outer joins where I grab instances of type Abstract (in reality it is twelve). What I realized yesterday is that ConcreteTypeA1 and ConcreteTypeA2 really have the same extra data, they just behave differently, so what I would like to do is reduce the number of joins by stuffing these two classes into one table and using a discriminator column. How / can I accomplish this?

class Abstract {
    /* common data stored in abstract */
}

abstract class ConcreteTypeA extends Abstract {
    /* extra data stored in abstract_type_a */
}

class ConcreteTypeA1 extends ConcreteTypeA {
    /* just behavior, no extra data, uses data in abstract_type_a */
}

class ConcreteTypeA2 extends ConcreteTypeA {
    /* just behavior, no extra data, uses data in abstract_type_a */
}

class ConcreteTypeB extends Abstract {
    /* extra data stored in concrete_type_b */
}

Answer

Used answer from this question as per Vincents’ advice.

How to mix inheritance strategies with JPA annotations and Hibernate?

Leave a Reply

Your email address will not be published. Required fields are marked *