【发布时间】:2016-02-07 12:48:04
【问题描述】:
我正在使用 EclipseLink 将一些数据库方法从 Hibernate 迁移到 JPA,并且使用 Set 中的 .size 方法对结果进行排序的 NamedQuery 存在问题。 这是我的 Entity 类,其中包含 Query 和导致问题的字段:
@Entity
@Table(name = "PRODUCT")
@DiscriminatorValue(value = "Bundle")
@NamedQueries({@NamedQuery(name = BundleProduct.findProductBundles, query = "select distinct bundle from BundleProduct bundle inner join bundle.products as product inner join bundle.services as service where service.name = :service and product in (:products) and not exists (select product2 from BundleProduct bundle2 join bundle2.products as product2 where bundle = bundle2 and product2 not in (:products)) order by product.size desc, bundle.name")})
public class BundleProduct extends Product
{
@ManyToMany
@JoinTable(name = "SERVICEPRODUCT",
joinColumns = {@JoinColumn(name = "ITSPRODUCT")},
inverseJoinColumns = {@JoinColumn(name = "ITSSERVICE")})
private Set<SingleProduct> products;
这会导致我的应用服务器 (Weblogic 12.2.1.0.0) 出现异常:
Exception Description: Deployment of PersistenceUnit [myunit] failed. Close all factories for this PersistenceUnit.
Internal Exception: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.6.1.v20150916-55dc7c3): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Problem compiling [select distinct bundle from BundleProduct bundle join bundle.products as product join bundle.services as service where service.name = :service and product in (:products) and not exists (select product2 from BundleProduct bundle2 join bundle2.products as product2 where bundle = bundle2 and product2 not in (:products)) order by product.size desc, bundle.name]. [349, 361] The state field path 'product.size' cannot be resolved to a valid type.
“order by”子句的第一部分曾经是“bundle.products.size”,我认为将其更改为“product.size”应该可以解决问题,但我仍然得到异常。
在我研究这个问题的过程中,我发现了多个帖子,IDE 似乎可以自动完成这些查询,但后来它不起作用。我正在使用 IDEA 14.1.5。
【问题讨论】:
-
为什么不使用
count()?这听起来更像是一种 sql-ish 方法...... -
这是 JPQL,而不是 SQL,所以我使用的是实体,而不是数据库表和列。
-
@phivo 上面的评论仍然有效,因为JPQL是直接翻译成SQL的,所以有
GROUP BY和COUNT这样的关键字。 -
仅仅因为“size”字段不存在并不意味着 SIZE(...) 在 JPQL 中不存在 ... 查看答案。这不是从根本上改变您的查询的理由
标签: java jpa eclipselink jpql named-query