JPA 2.1条件API:在orderBy

时间:2015-10-26 13:54:10

标签: java spring hibernate jpa criteria-api

我试图在子查询的帮助下对查询进行排序,但总是得到一个

  

org.springframework.dao.InvalidDataAccessApiUsageException:   org.hibernate.hql.internal.ast.QuerySyntaxException:意外的AST   节点

E.g。

@Entity
@Table
public class Product extends AbstractEntity {
     @OneToMany(mappedBy = "product")
     private List<Item> items = new ArrayList<>();
}

@Entity
public class Item extends AbstractEntity {
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_id")
    private Product product;

    @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
    @Column(name = "delivery_date")
    private DateTime deliveryDate;
}

我想查询所有产品,并按照其最早的deliveryDate项目订购结果。我按如下方式构建查询:

CriteriaBuilder builder = manager.getCriteriaBuilder();
CriteriaQuery<Product> query = builder.createQuery(Product.class);
Root<Product> product = query.from(Product.class);

Subquery<DateTime> deliveryDateSQ = query.subquery(DateTime.class);
Root<Item> itemsFrom = deliveryDateSQ.from(Item.class);
deliveryDateSQ.select(
    criteriaBuilder.least(itemsFrom.get(Item_.deliveryDate)));
deliveryDateSQ.where(
    criteriaBuilder.equal(itemsFrom.get(Item_.product), product));

query.orderBy(criteriaBuilder.asc(deliveryDateSQ));

TypedQuery<Product> typedQuery = manager.createQuery(query);

执行查询时,我总是会收到以下错误:

antlr.NoViableAltException: unexpected AST node: query
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.orderExpr(HqlSqlBaseWalker.java:1836) [hibernate-core-4.3.11.Final.jar:4.3.11.Final]
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.orderExprs(HqlSqlBaseWalker.java:1627) [hibernate-core-4.3.11.Final.jar:4.3.11.Final]
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.orderClause(HqlSqlBaseWalker.java:1603) [hibernate-core-4.3.11.Final.jar:4.3.11.Final]
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:642) [hibernate-core-4.3.11.Final.jar:4.3.11.Final]
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:301) [hibernate-core-4.3.11.Final.jar:4.3.11.Final]
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:249) [hibernate-core-4.3.11.Final.jar:4.3.11.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:278) [hibernate-core-4.3.11.Final.jar:4.3.11.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:206) [hibernate-core-4.3.11.Final.jar:4.3.11.Final]

Spring的一些细节:

org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected AST node: query [...]
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:381)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:216)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:417)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)

有没有办法以某种方式更改查询以使其按预期工作?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我们可以使用 @OrderBy(子句=&#34; delivery_date ASC&#34;) Hibernate子句。

所以你的模型可以是这样的。

@Entity
@Table
public class Product extends AbstractEntity {
     @OrderBy(clause = "delivery_date ASC")
     @OneToMany(mappedBy = "product")
     private List<Item> items = new ArrayList<>();
}

@Entity
public class Item extends AbstractEntity {
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_id")
    private Product product;

    @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
    @Column(name = "delivery_date")
    private DateTime deliveryDate;
}