gpt4 book ai didi

sql - 如何在 JPQL 中将日期时间转换为日期?

转载 作者:行者123 更新时间:2023-12-04 16:27:55 32 4
gpt4 key购买 nike

这段代码有什么问题?

@Query(value = "Select " +
"date(ivd.trnDatetime) as date, " +
"ivd.binNo as bin, " +
"ivd.snNo as sn, " +
"count(ivd.invoiceNo) as totInvoice, " +
"sum(ivd.totSaleAmt) as totSaleAmt " +
"from InvoiceData ivd where ivd.status = 1 group by date(ivd.trnDatetime), ivd.binNo, ivd.snNo")
List<Object[]> getDailyTransactionReport();

它抛出
    java.lang.IllegalArgumentException: org.hibernate.QueryException: No data type for node: org.hibernate.hql.internal.ast.tree.MethodNode 
+-[METHOD_CALL] MethodNode: '('
| +-[METHOD_NAME] IdentNode: 'date' {originalText=date}
| \-[EXPR_LIST] SqlNode: 'exprList'
| \-[DOT] DotNode: 'invoicedat0_.transaction_at' {propertyName=trnDatetime,dereferenceType=PRIMITIVE,getPropertyPath=trnDatetime,path=ivd.trnDatetime,tableAlias=invoicedat0_,className=com.efdms.transactionmonitoring.domain.InvoiceData,classAlias=ivd}
| +-[ALIAS_REF] IdentNode: 'invoicedat0_.id' {alias=ivd, className=com.efdms.transactionmonitoring.domain.InvoiceData, tableAlias=invoicedat0_}
| \-[IDENT] IdentNode: 'trnDatetime' {originalText=trnDatetime}
[Select date(ivd.trnDatetime) as date, ivd.binNo as bin, ivd.snNo as sn, count(ivd.invoiceNo) as totInvoice, sum(ivd.totSaleAmt) as totSaleAmt from com.efdms.transactionmonitoring.domain.InvoiceData ivd where ivd.status = 1 group by date(ivd.trnDatetime), ivd.binNo, ivd.snNo]
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:138)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:188)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:729)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:351)
at com.sun.proxy.$Proxy190.createQuery(Unknown Source)
at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:87)
... 62 common frames omitted
trnDatetime的属性在实体
@NotNull
@Column(name = "transaction_at", nullable = false)
private Instant trnDatetime;

SqlServer 中的等效列是 datetime

最佳答案

JPQL 不支持 date()所以你的查询失败了。你有以下选项:

  • 通过设置 nativeQuery=true 更改为使用 native SQL在 @Query
  • 如果您使用的是 Hibernate,请使用 HQL 特定的 cast() 那个 Actor trnDatetimeLocalDate :

  • select cast(ivd.trnDatetime as LocalDate) as date ,
    count(ivd.invoiceNo) as totInvoice
    from InvoiceData ivd
    where ivd.status = 1
    group by cast(ivd.trnDatetime as LocalDate)"

    为简洁起见,我忽略了其他列

    请注意,结果列表的每一项中的索引 0 是一个 LocalDate

    关于sql - 如何在 JPQL 中将日期时间转换为日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59124239/

    32 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com