EclipseLink:对MappedSuperclass的查询失败 [英] EclipseLink: Query to MappedSuperclass fails
问题描述
我的应用程序是一家销售鱼类,水族馆等的商店。我想根据销售数量获得所有商品中前10项的清单。我使用以下类:
My application is a store selling fishes, aquariums etc. I want to get a list of top 10 items among all the items based on sales count. I use the following class:
@MappedSuperclass
@NamedQueries({
@NamedQuery(name="getTopItems",query="SELECT x FROM FishStoreItem x ORDER BY x.salescnt DESC, x.title DESC")
})
public abstract class FishStoreItem
extends DomainSuperClass implements Serializable {
......
}
问题出现在以下异常中:
Problem is in the following exception:
异常[EclipseLink-8034](Eclipse
持久性服务 -
2.0.0.v20091127-r5931):org。 eclipse.persistence.exceptions.JPQLException
异常描述:编译
查询时出错[getTopItems:SELECT x FROM
FishStoreItem x ORDER BY x.salescnt
DESC,x.title DESC] 。未知实体
类型[FishStoreItem]。
Exception [EclipseLink-8034] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.JPQLException Exception Description: Error compiling the query [getTopItems: SELECT x FROM FishStoreItem x ORDER BY x.salescnt DESC, x.title DESC]. Unknown entity type [FishStoreItem].
相同的代码适用于Apache OpenJpa 2.0.0,但在EclipseLink版本中失败2.1.0,2.0.1,1.0。
Same code works fine with Apache OpenJpa 2.0.0, but fails with EclipseLink ver 2.1.0, 2.0.1, 1.0.
PS我已经找到了针对Hibernate的那个解决方案,但我想确定EclipseLink也是不可能的。
P.S. I've already found that solution for Hibernate, but I want to be sure that it is impossible for EclipseLink too.
推荐答案
映射的超类不是实体,不允许查询,持久化或关系到超类(参见映射超类)。
A mapped superclass is not an entity and does not allow querying, persisting, or relationships to the superclass (see Mapped Superclasses).
这在Apache OpenJPA 2.0用户指南中得到确认:
This is confirmed in Apache OpenJPA 2.0 User's Guide:
1.3。映射的超类
映射的超类是一个非实体
类,可以定义持久状态
和实体
子类的映射信息。映射的超类通常是抽象的
。与真正的
实体不同,您无法查询映射的
超类,将映射的超类
实例传递给任何EntityManager
或
查询方法,或声明与映射的超类
目标的持久
关系。您使用MappedSuperclass
标记
注释表示映射的超类
。
1.3. Mapped Superclass
A mapped superclass is a non-entity class that can define persistent state and mapping information for entity subclasses. Mapped superclasses are usually abstract. Unlike true entities, you cannot query a mapped superclass, pass a mapped superclass instance to any
EntityManager
or Query methods, or declare a persistent relation with a mapped superclass target. You denote a mapped superclass with theMappedSuperclass
marker annotation.
...
注意
OpenJPA允许您查询映射的
超类。对映射的
超类的查询将返回所有匹配的
子类实例。 OpenJPA也
允许你声明与
映射的超类类型的关系;但是,你
无法查询这些关系。
OpenJPA allows you to query on mapped superclasses. A query on a mapped superclass will return all matching subclass instances. OpenJPA also allows you to declare relations to mapped superclass types; however, you cannot query across these relations.
因此,虽然OpenJPA允许您的查询,但这超出了JPA。不要指望它适用于任何JPA提供商。
So, while OpenJPA allows your query, this is beyond JPA. Don't expect it to work with any JPA provider.
这篇关于EclipseLink:对MappedSuperclass的查询失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!