Say we've got an abstract @Entity Animal, and several entity classes that extend Animal, including Dog, Cat, Monkey and Bat.
假设我们有一个抽象的@Entity Animal,以及几个扩展了Animal的实体类,包括Dog、Cat、Monkey和Bat。
How can I filter the results based on the extending entity's class?
如何根据扩展实体的类过滤结果?
Example: There are checkboxes where the user can select which entities to retrieve.
示例:有一些复选框,用户可以在其中选择要检索的实体。
[ ] Dog
[X] Cat
[X] Monkey
[ ] Bat
Now I want to retrieve the entities with a (Named)Query defined in the Animal
class. What kind of query parameters can I put into the query so that only the Cat and Monkey objects will be returned?
现在,我希望检索在动物类中定义的(命名)查询的实体。我可以将什么样的查询参数放入查询中,以便只返回Cat和Monkey对象?
2 个解决方案
#1
29
I'm not absolutely sure it's supported by JPA, but the way to do it in Hibernate, regardless of the inheritance strategy, and thus even if you don't have a discriminator (or didn't map it as a property) is to use the implicit class
property :
我不确定JPA是否支持它,但是不管继承策略如何,在Hibernate中实现它的方法是使用隐式类属性:
String jpql = "select a from Animal a where a.class in (:classes)";
Query q = em.createQuery(jpql).setParameter("classes",
Arrays.asList(Cat.class, Monkey.class));
EDIT :
编辑:
I just found it's possible in JPA2 using the TYPE operator :
我刚刚发现JPA2可以使用类型运算符:
String jpql = "SELECT a FROM Animal a WHERE TYPE(a) IN :classes";
Query q = em.createQuery(jpql).setParameter("classes",
Arrays.asList(Cat.class, Monkey.class));
#2
4
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"
可以使用discrimnator列和value只搜索给定类型的某些子类型。默认情况下,discriminator列的名称是JPA中的DTYPE,类型是String,值是类的名称。但是,您可以通过添加类级注释@DiscriminatorColumn(name=“KIND”,discriminatorType= discriminatorType . integer)(用于discriminator列的名称和类型)和@DiscriminatorValue(“1”)(用于特定类的特定discrimiminator值)来覆盖这个属性。然后,您可以在yoru JPQL查询的WHERE子句中使用它来只获取某些子类型,比如:WHERE DTYPE="Dog"或DTYPE="Cat"
#1
29
I'm not absolutely sure it's supported by JPA, but the way to do it in Hibernate, regardless of the inheritance strategy, and thus even if you don't have a discriminator (or didn't map it as a property) is to use the implicit class
property :
我不确定JPA是否支持它,但是不管继承策略如何,在Hibernate中实现它的方法是使用隐式类属性:
String jpql = "select a from Animal a where a.class in (:classes)";
Query q = em.createQuery(jpql).setParameter("classes",
Arrays.asList(Cat.class, Monkey.class));
EDIT :
编辑:
I just found it's possible in JPA2 using the TYPE operator :
我刚刚发现JPA2可以使用类型运算符:
String jpql = "SELECT a FROM Animal a WHERE TYPE(a) IN :classes";
Query q = em.createQuery(jpql).setParameter("classes",
Arrays.asList(Cat.class, Monkey.class));
#2
4
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"
可以使用discrimnator列和value只搜索给定类型的某些子类型。默认情况下,discriminator列的名称是JPA中的DTYPE,类型是String,值是类的名称。但是,您可以通过添加类级注释@DiscriminatorColumn(name=“KIND”,discriminatorType= discriminatorType . integer)(用于discriminator列的名称和类型)和@DiscriminatorValue(“1”)(用于特定类的特定discrimiminator值)来覆盖这个属性。然后,您可以在yoru JPQL查询的WHERE子句中使用它来只获取某些子类型,比如:WHERE DTYPE="Dog"或DTYPE="Cat"