English 中文(简体)
Hibernate鉴别器列,每个子类有表
原标题:Hibernate discriminator column with table per subclass

现在我正在使用每子类一个表的方法来建模我的数据。我的层次结构简化为:

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

所以它做了三个外部连接,在那里我获取抽象类型的实例(实际上是十二个)。我昨天意识到,ConcreteTypeA1和ConcreteType A2确实有相同的额外数据,它们只是表现不同,所以我想做的是通过将这两个类填充到一个表中并使用鉴别器列来减少联接的数量。我怎样才能做到这一点?

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 */
}
最佳回答

根据Vincents的建议使用了这个问题的答案。

如何将继承策略与JPA注释和Hibernate混合使用

问题回答

在父类上使用此

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

和混凝土类使用

@DiscriminatorValue("TypeA")




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签