QueryDSL JPA函数

时间:2017-08-04 06:52:02

标签: java jpa querydsl

我使用querydsl-jpa和querydsl-sql 4.1.4

想要运行简单的sql

/* Formatted on 04/08/2017 9:46:39 (QP5 v5.300) */
SELECT created
  FROM user_info
 WHERE TRUNC (created) <= TO_DATE ('01.01.2016', 'dd.mm.yyyy')

我的方法比较日期

protected BooleanExpression dateCompare(DateTimePath<java.util.Date> dateTimePath, Date date) {
    DateExpression<Date> dbDate = SQLExpressions.datetrunc(DatePart.day, Expressions.asDate(dateTimePath));
    DateExpression<Date> compareDate = Expressions. asDate(date);
    return dbDate.eq(compareDate);
}

但我有错误

Caused by: java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: 
Exception Description: Syntax error parsing [select transferLog
from TransferLog transferLog
where trunc_day(transferLog.transaction) = ?1]. 
[54, 93] The expression is not a valid conditional expression.
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1616)

如何在QueryDSL中正确使用TRUNC和TO_DATE ???

例如在JPA中我使用这样的代码

if (transferFilter.getTransactionEnd() != null) {
            Expression<Date> transactionEnd = qb.function("TRUNC", Date.class, root.get(TransferLog_.transaction));
            Expression<Date> tillDate = qb.function("TO_DATE", Date.class, qb.literal(DateUtils.formatRuDate(transferFilter.getTransactionEnd(), "")), qb.literal("dd.mm.yyyy"));
            predicates.add(qb.greaterThanOrEqualTo(transactionEnd, tillDate));
            }

我尝试使用这样的

protected BooleanExpression dateCompare(DateTimePath<java.util.Date> dateTimePath, Date date) {
    DateExpression<Date> dbDate = Expressions.dateTemplate(Date.class, "TRUNC({0})", dateTimePath);
    DateExpression<Date> compareDate = Expressions.dateTemplate(Date.class, "TO_DATE({0}, {1})", DateUtils.formatRuDate(date), ORACLE_DATE_FORMAT);
    // DateExpression<Date> dbDate = SQLExpressions.datetrunc(DatePart.day, Expressions.asDate(dateTimePath));
    // DateExpression<Date> compareDate = Expressions. asDate(date);
    return dbDate.eq(compareDate);
}

但有错误

Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.6.4.v20160829-44060b6): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Syntax error parsing [select transferLog
from TransferLog transferLog
where TRUNC(transferLog.transaction) = TO_DATE(?1, ?2)]. 
[54, 102] The expression is not a valid conditional expression.
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildException(HermesParser.java:155)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.validate(HermesParser.java:334)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.populateQueryImp(HermesParser.java:278)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildQuery(HermesParser.java:163)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:142)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:116)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:102)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:86)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1614)
    ... 219 more

1 个答案:

答案 0 :(得分:1)

我决定这样......

    protected enum TypeCompare {
                                EQ,
                                GT,
                                GOE,
                                LT,
                                LOE
    }
    private final static String ORACLE_DATE_FORMAT = "dd.mm.yyyy";

...

    protected BooleanExpression dateCompare(DateTimePath<java.util.Date> dateTimePath, Date date, TypeCompare typeCompare) {
        StringTemplate dbDate = Expressions.stringTemplate("function('TRUNC', {0})", dateTimePath);
        StringTemplate compareDate = Expressions.stringTemplate("function('TO_DATE', {0}, {1})", DateUtils.formatRuDate(date), ORACLE_DATE_FORMAT);
        switch (typeCompare) {
            case EQ:
                return dbDate.eq(compareDate);
            case GT:
                return dbDate.gt(compareDate);
            case GOE:
                return dbDate.goe(compareDate);
            case LT:
                return dbDate.lt(compareDate);
            case LOE:
                return dbDate.loe(compareDate);
            default:
                return dbDate.eq(compareDate);
        }
    }