Search code examples
javajpaeclipselinkjpqlnamed-query

EclipseLink JPA NamedQuery can not find .size Attribute in field of type Set


I'm migrating some database methods from Hibernate to JPA with EclipseLink and have an issue with one NamedQuery that used the .size Method from a Set for ordering the results. Here's my Entity class with the Query and the field that's causing the problem:

@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;

This causes an Exception on my Application Server (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.

The first part of the "order by" clause used to be "bundle.products.size" and I was under the impression that changing this to "product.size" should solve the issue but I still get the Exception.

During my research of this issue I found multiple posts that IDEs seem to autocomplete these queries but later it doesn't work. I'm using IDEA 14.1.5.


Solution

  • The state field path 'product.size' cannot be resolved to a valid type.

    Yes "size" does not exist, BUT JPQL has SIZE(...) to handle just this.

    ORDER BY SIZE(products) DESC, bundle.name
    

    As always, refer to a JPQL reference document to see what functions are available, such as this one.