How to create an 'instance of' -like query in JPA 2.0?

后端 未结 2 1747
天命终不由人
天命终不由人 2020-12-30 06:30

Say we\'ve got an abstract @Entity Animal, and several entity classes that extend Animal, including Dog, Cat, Monkey and Bat.

How can I filter the results based on t

2条回答
  •  伪装坚强ぢ
    2020-12-30 06:58

    You can use the discrimnator column and value to only search for certain subtypes of a given type. By default the discriminator column's name is DTYPE in JPA,the type is String and the value is the name of the class. You can however override this by adding the class level annotation @DiscriminatorColumn(name="KIND", discriminatorType=DiscriminatorType.INTEGER) (for the discriminator column's name and type) and @DiscriminatorValue("1") (for the specific discrimiminator value for a certain class). You can then use this in the WHERE clause of yoru JPQL query to only fetch certain subtypes, like: WHERE DTYPE="Dog" OR DTYPE="Cat"

提交回复
热议问题