vect
vect

Reputation: 665

How to get parent entity?

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?

Upvotes: 0

Views: 743

Answers (2)

vect
vect

Reputation: 665

Problem is solved. I've annotated root entity class of inheritance hierarchy (CardElement) in this way: @DiscriminatorValue(value = "not null"). Now I can select objects of this class without creating subclass for each discriminator value. not null and null seem to be Hibernate's special discriminator values which match in discriminator column anything except null and null respectively. I've not found any information about these values in Hibernate's official documentation. So it could be some kind of undocumented feature.

Upvotes: 0

Adrian Shum
Adrian Shum

Reputation: 40036

I am not sure if I understand your problem correctly. You are using Single Table strategy to store the whole inheritance hierarchy. However, you have only mapped some of the discriminators, and this time, it is the unmapped discriminator causing the problem (because Hibernate dunno what that subclass means). Am I understanding your problem correctly?

Consider work against a special DB view instead of the real table. That view expose only records with discriminator you can handle.

Upvotes: 1

Related Questions