开发者

Hibernate discriminator column with table per subclass

开发者 https://www.devze.com 2023-03-10 04:16 出处:网络
Right now I am using a table per subclass approach to model my data.A simplification of my hierarchy is:

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 */
}


use this on Parent class

@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
    name="type",
    discriminatorType=DiscriminatorType.STRING)

and on concrete classes use

@DiscriminatorValue("TypeA")


Used answer from this question as per Vincents' advice.

How to mix inheritance strategies with JPA annotations and Hibernate?

0

精彩评论

暂无评论...
验证码 换一张
取 消