mjs
mjs

Reputation: 22369

Hibernate/JPA: How to find sub entities using InheritanceType.JOINED

Can you, given a SuperEntity which is extended by three sub entities SubEntityA, SubEntityB and SubEntityC, perform a criteria query on SuperEntity to retrieve only instances of SubEntityB and SubEntityC?

With InheritanceType.SINGLE_TABLE you can set a discriminator value which I believe is used to query. How is it done with the InheritanceType.JOINED?

Upvotes: 0

Views: 1191

Answers (1)

JimmyB
JimmyB

Reputation: 12610

a) You can include (redundant) @DiscriminatorColumn/@DiscriminatorValues in a JOINED entity hierarchy too.

b) You can use the JPQL type operator: http://en.wikibooks.org/wiki/Java_Persistence/JPQL#JPQL_special_operators , like SELECT se FROM SuperEntity se WHERE TYPE(se) <> SubEntityA

c) Use multiple queries (SELECT se FROM SubEntityB se + SELECT se FROM SubEntityC se) to collect all entities of interest.

Upvotes: 3

Related Questions