1

I am using spring data jpa. I am using InheritanceType.JOINED for my inheritance strategy. Say I have the following entity relationships:

Animal (abstract class)
Cow extends Animal
Pig extends Animal
Cat extends Animal
etc..

Each animal has a "TYPE" column populated with the name of the class.

How would I query for some subset of animal types without joining every subclass table? For example, if I wanted to retrieve all cows and pigs but not any cats.

I have tried @Query("select a from Animal where a.type in (Cow, Pig)"), but this will still perform a LEFT JOIN on the Cat table which is undesirable.

Is there some workaround for this?

  • Couldn't you only use one table? Or are the fields between classes that different? – Lino Feb 28 '22 at 21:49
  • @Lino Yes, I believe the "best" solution is to use a single_table inheritance strategy, but unfortunately I am not able to change how the tables are structured at this point. Also, for this case, yes the fields between classes are that different – FatherIceman Feb 28 '22 at 22:00

0 Answers0