本文介绍了QueryDSL JPA功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用querydsl-jpa和querydsl-sql 4.1.4
I use querydsl-jpa and 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?
How to correctly use TRUNC and TO_DATE in QueryDSL???
例如在JPA中,我使用此类代码
For example in JPA i Use such code
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
推荐答案
我决定是这样的...
I decided this so ...
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);
}
}
这篇关于QueryDSL JPA功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!