在Hibernate中的generate_series函数

huangapple go评论94阅读模式
英文:

generate_series function in Hibernate

问题

我有一个表格 Inv 里的列 total,我想要使用 Hibernate 提取每个月的总金额,使用 generate_series 函数:

@Query(value = "SELECT _month," +
            "sum(inv.total) AS total" +
            "FROM (FUNCTION('generate_series', 1, 12, 1)) as _month" +
            "join InvSC as sc on FUNCTION('extract', month, sc.timestamp) = _month" +
            "JOIN Inv as inv on sc.invoiceNumber = inv.invoiceNumber" +
            "GROUP BY _month")
List<String> totalAmount();

我遇到了语法错误:

Caused by: org.hibernate.query.sqm.ParsingException: line 1:58 mismatched input 'FUNCTION' expecting {<EOF>, ',', FROM, GROUP, ORDER, WHERE}
	at org.hibernate.query.hql.internal.StandardHqlTranslator$1.syntaxError(StandardHqlTranslator.java:46) ~[hibernate-core-6.1.7.Final.jar:6.1.7.Final]

我应该如何使用 FROM (FUNCTION ('generate_series')) 编写查询?

英文:

I have a column total in the table Inv and I want to extract total amount per month in hibernate using generate_series:

@Query(value = &quot;SELECT _month,&quot; +
            &quot;sum(inv.total) AS total&quot; +
            &quot;FROM FUNCTION(&#39;generate_series&#39;, 1, 12, 1) as _month&quot; +
            &quot;join InvSC as sc on FUNCTION(&#39;extract&#39;, month, sc.timestamp) = _month&quot; +
            &quot;JOIN Inv as inv on sc.invoiceNumber = inv.invoiceNumber&quot; +
            &quot;GROUP BY _month&quot;)
    List&lt;String&gt; totalAmount();

I have syntax error

Caused by: org.hibernate.query.sqm.ParsingException: line 1:58 mismatched input &#39;FUNCTION&#39; expecting {&lt;EOF&gt;, &#39;,&#39;, FROM, GROUP, ORDER, WHERE}
	at org.hibernate.query.hql.internal.StandardHqlTranslator$1.syntaxError(StandardHqlTranslator.java:46) ~[hibernate-core-6.1.7.Final.jar:6.1.7.Final]

How can I write the query using FROM (FUNCTION (&#39;generate_series&#39;))?

答案1

得分: 1

I. 创建带有单行和相应虚拟实体的虚拟表:

create table dual(dummy varchar(1));
insert into dual(dummy) values('x');
@Entity(name = "dual")
@Table(name = "dual")
public class Dual {

    @Id
    @Column(name = "dummy")
    protected String dummy;
    
}

II. 现在,您可以在JPQL中编写类似以下的代码:

WITH mon AS MATERIALIZED (
   SELECT FUNCTION('generate_series', 1, 12, 1) AS month_ FROM dual
)
SELECT mon.month_, sum(inv.total) AS total
FROM mon, InvSC as sc, Inv as inv
WHERE sc.invoiceNumber = inv.invoiceNumber
AND mon.month_ = FUNCTION('extract', 'month', sc.timestamp)
GROUP BY mon.month_
英文:

Since Hibernate 6.2 introduced support of common table expressions, I believe, you can call table functions using following approach:

I. create dummy table with single row and corresponding dummy entity

create table dual(dummy varchar(1));
insert into dual(dummy) values(&#39;x&#39;);
@Entity(name = &quot;dual&quot;)
@Table(name = &quot;dual&quot;)
public class Dual {

    @Id
    @Column(name = &quot;dummy&quot;)
    protected String dummy;
    
}

II. Now, you are able to write something like in JPQL:

WITH mon AS MATERIALIZED (
   SELECT FUNCTION(&#39;generate_series&#39;, 1, 12, 1) AS month_ FROM dual
)
SELECT mon.month_, sum(inv.total) AS total
FROM mon, InvSC as sc, Inv as inv
WHERE sc.invoiceNumber = inv.invoiceNumber
AND mon.month_ = FUNCTION(&#39;extract&#39;, &#39;month&#39;, sc.timestamp)
GROUP BY mon.month_

huangapple
  • 本文由 发表于 2023年6月25日 23:51:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76551244.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定