java JPA(和继承)如何获取给定超类的所有实体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6650768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 16:48:17  来源:igfitidea点击:

JPA (and inheritance) how do I get all entities of a given superclass

javajpa

提问by Sean Patrick Floyd

Given the following entity definitions:

鉴于以下实体定义:

@Entity
class abstract A {
    Collection<A> parents;
}


@Entity
class B extends A {

}


@Entity
class C extends A {

}

is it possible to define a method which returns all entities of type B & C having a given parent, withouthaving to make two separate calls and then merging the results?

是否可以定义一个方法来返回所有具有给定父级的 B 和 C 类型的实体,不必进行两次单独的调用然后合并结果?

Collection<A> getAllByParentId(long id)

回答by Sean Patrick Floyd

It should be as simple as this:

它应该像这样简单:

List<A> results = entityManager
                      .createQuery("Select a from A a", A.class)
                      .getResultList();