我正在使用 EclipseLink 将一些数据库方法从 Hibernate 迁移到 JPA,并且遇到一个使用 Set 中的 .size 方法对结果进行排序的 NamedQuery 的问题。 这是我的实体类,其中包含查询和导致问题的字段:
@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。
最佳答案
The state field path 'product.size' cannot be resolved to a valid type.
是的,“size”不存在,但是 JPQL 有 SIZE(...) 来处理这个问题。
ORDER BY SIZE(products) DESC, bundle.name
一如既往,请参阅 JPQL 引用文档以查看可用的函数,例如 this one .
关于java - EclipseLink JPA NamedQuery 在 Set 类型的字段中找不到 .size 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33565662/