0

I use JPA 2 with Hibernate. There are two entity classes, CardElement and IdentityDocumentKind. The last one is an inherited entity of the first one. SINGLE_TABLE inheritance strategy is used. When I try to select an instance of the parent class by query from CardElement where id = '123456' the following error occures:

Object with id: 123456 was not of the specified subclass: org.cp.cardsystem.CardElement (Discriminator: SDocClass)

I don't have a subclass for "SDocClass" discriminator value. Actually at the moment of developing IdentityDocumentKind class querying of CardElement was used widely across the application. So I can't create a subclass of CardElement for each discriminator value and replace CardElement with it in all existent queries. It would be cost too much efforts for me. Is there a way to instantiate parent entity class when SINGLE_TABLE inheritance strategy is used?

4

2 回答 2

1

我不确定我是否正确理解了您的问题。您正在使用单表策略来存储整个继承层次结构。但是,您只映射了一些鉴别器,而这一次,是未映射的鉴别器导致了问题(因为 Hibernate 不知道该子类的含义)。我是否正确理解您的问题?

考虑针对特殊的数据库视图而不是真实表工作。该视图仅公开具有您可以处理的鉴别器的记录。

于 2012-08-07T04:05:37.630 回答
0

问题解决了。我CardElement以这种方式注释了继承层次结构的根实体类 ( ):@DiscriminatorValue(value = "not null"). 现在我可以选择此类的对象,而无需为每个鉴别器值创建子类。not null并且null似乎是 Hibernate 的特殊鉴别器值,它们在鉴别器列中匹配除 null 和 null 之外的任何内容。我在 Hibernate 的官方文档中没有找到关于这些值的任何信息。所以它可能是某种未记录的功能。

于 2012-08-07T15:27:15.680 回答