问题描述
现在我使用每个子类的表来模拟我的数据。简化我的层次结构为:
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ / *常见数据存储在abstract * /
class ConcreteTypeA1 extends Abstract {
/ *存储在concrete_type_a_1中的额外数据* /
}
class ConcreteTypeA2 extends Abstract {
/ *存储在concrete_type_a_2中的额外数据* /
}
class ConcreteTypeB扩展Abstract {
/ *存储在concrete_type_b *中的额外数据* /
}
因此,它会执行三个外部连接,用于抓取Abstract类型的实例(实际上它是十二个)。我昨天意识到ConcreteTypeA1和ConcreteTypeA2确实具有相同的额外数据,它们只是行为不同,所以我想要做的是通过将这两个类填充到一个表中并使用鉴别器列来减少联接的数量。
class抽象{
/ *存储在摘要中的常用数据* /
}
抽象类ConcreteTypeA extends抽象{
/ *存储在abstract_type_a * /
中的额外数据}
class ConcreteTypeA1 extends ConcreteTypeA {
/ *只是行为,没有额外的数据,使用abstract_type_a中的数据* /
}
class ConcreteTypeA2 extends ConcreteTypeA {
/ *只是行为,没有额外的数据, abstract_type_a * /
}
class ConcreteTypeB extends Abstract {b $ b / *存储在concrete_type_b中的额外数据* /
}
解决方案根据Vincents的建议,从这个问题中回答问题。
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 */ }
解决方案Used answer from this question as per Vincents' advice.
How to mix inheritance strategies with JPA annotations and Hibernate?
这篇关于Hibernate的鉴别器列与每个子类的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!