简体   繁体   English

查询映射的超类或等效类

[英]Querying Mapped Superclasses or Equivalent

I have three classes that are subclasses of the abstract superclass Automobile. 我有三个类,它们是抽象超类Automobile的子类。 I'm using the single table inheritance model and a @MappedSuperclass for the automobile class. 我正在使用单表继承模型和用于汽车类的@MappedSuperclass The subclasses are Car, Truck, and Van. 子类是Car,Truck和Van。

I want to query the automobile class, but have the different subclasses returned. 我想查询汽车类,但返回了不同的子类。 I've written a couple queries and done some research, but it seems like it is not possible to query against MappedSuperclasses. 我已经编写了一些查询并进行了一些研究,但似乎无法针对MappedSuperclasses进行查询。 I have also tried to do NativeSqlQueries, but I can't seem to be able to figure out how to specifiy multiple ResultSetMappings. 我也尝试过执行NativeSqlQueries,但似乎无法弄清楚如何指定多个ResultSetMappings。

Is there anyway to accomplish this? 反正有做到这一点吗?

You cannot use a mapped-superclass in the query. 您不能在查询中使用映射超类。 If you want to use Automobile in the query, don't mark it as mapped-superclass, instead mark it as an entity. 如果要在查询中使用Automobile ,请不要将其标记为映射超类,而应将其标记为实体。

Following is from the JPA 2.0 Spec , second paragraph is what's relevant in your case - 以下是来自JPA 2.0规范的内容 ,第二段与您的案例有关-

2.11.2 Mapped Superclasses 2.11.2映射的超类

An entity may inherit from a superclass that provides persistent entity state and mapping information, but which is not itself an entity. 实体可以从提供持久实体状态和映射信息的超类继承,但它本身不是实体。 Typically, the purpose of such a mapped superclass is to define state and mapping information that is common to multiple entity classes. 通常,此类映射超类的目的是定义多个实体类共有的状态和映射信息。

A mapped superclass, unlike an entity, is not queryable and must not be passed as an argument to EntityManager or Query operations. 与实体不同,映射的超类不可查询,并且不得将其作为参数传递给EntityManager或Query操作。 Persistent relationships defined by a mapped superclass must be unidirectional. 映射超类定义的持久关系必须是单向的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM