QueryDsl SQLExpressions.datetrunc()不起作用

时间:2018-10-11 14:19:25

标签: java postgresql spring-boot jpa querydsl

我想在 Spring Boot 2.0.5.RELEASE 中使用SQLExpressions.datetrunc()方法。我在Postgres DB中使用 QueryDsl 4.1.4

这是我要获取的示例:

  public List<Date> getTruncatedDate(){
    final QFileInformation qFileInformation = QFileInformation.fileInformation;
    final JPAQuery<FileInformation> query = new JPAQuery<>(entityManager);

    return query.from(qFileInformation)
        .select(SQLExpressions.datetrunc(DatePart.day, qFileInformation.created))
        .fetch();
  }

我不断收到QueryException。这是异常输出:

2018-10-11 15:13:29.245 ERROR 3120 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: org.hibernate.QueryException: No data type for node: org.hibernate.hql.internal.ast.tree.MethodNode 
 \-[METHOD_CALL] MethodNode: '('
    +-[METHOD_NAME] IdentNode: 'trunc_day' {originalText=trunc_day}
    \-[EXPR_LIST] SqlNode: 'exprList'
       \-[DOT] DotNode: 'fileinform0_.created' {propertyName=created,dereferenceType=PRIMITIVE,getPropertyPath=created,path=fileInformation.created,tableAlias=fileinform0_,className=example.model.FileInformation,classAlias=fileInformation}
          +-[ALIAS_REF] IdentNode: 'fileinform0_.id' {alias=fileInformation, className=example.model.FileInformation, tableAlias=fileinform0_}
          \-[IDENT] IdentNode: 'created' {originalText=created}
 [select trunc_day(fileInformation.created)
from example.model.FileInformation fileInformation]] with root cause

org.hibernate.QueryException: No data type for node: org.hibernate.hql.internal.ast.tree.MethodNode 
 \-[METHOD_CALL] MethodNode: '('
    +-[METHOD_NAME] IdentNode: 'trunc_day' {originalText=trunc_day}
    \-[EXPR_LIST] SqlNode: 'exprList'
       \-[DOT] DotNode: 'fileinform0_.created' {propertyName=created,dereferenceType=PRIMITIVE,getPropertyPath=created,path=fileInformation.created,tableAlias=fileinform0_,className=example.model.FileInformation,classAlias=fileInformation}
          +-[ALIAS_REF] IdentNode: 'fileinform0_.id' {alias=fileInformation, className=example.model.FileInformation, tableAlias=fileinform0_}
          \-[IDENT] IdentNode: 'created' {originalText=created}

    at org.hibernate.hql.internal.ast.tree.SelectClause.initializeExplicitSelectClause(SelectClause.java:160) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.hql.internal.ast.HqlSqlWalker.useSelectClause(HqlSqlWalker.java:1013) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.hql.internal.ast.HqlSqlWalker.processQuery(HqlSqlWalker.java:781) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:677) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:313) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:261) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:266) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:189) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:141) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:115) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:77) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:153) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:553) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:662) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:23) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_162]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_162]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_162]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_162]
    at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:350) ~[spring-orm-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at com.sun.proxy.$Proxy112.createQuery(Unknown Source) ~[na:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_162]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_162]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_162]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_162]
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:305) ~[spring-orm-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at com.sun.proxy.$Proxy112.createQuery(Unknown Source) ~[na:na]
    at com.querydsl.jpa.impl.AbstractJPAQuery.createQuery(AbstractJPAQuery.java:101) ~[querydsl-jpa-4.1.4.jar:na]
    at com.querydsl.jpa.impl.AbstractJPAQuery.createQuery(AbstractJPAQuery.java:94) ~[querydsl-jpa-4.1.4.jar:na]
    at com.querydsl.jpa.impl.AbstractJPAQuery.fetch(AbstractJPAQuery.java:201) ~[querydsl-jpa-4.1.4.jar:na]

作为示例,我也尝试过使用addDay()方法,但是我一直收到相同的异常。当我直接在数据库管理工具中编写SQL查询时,它运行良好:

select date_trunc('day', created) from file_information;

有人知道发生这种情况的原因吗?

0 个答案:

没有答案