日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

本文介紹了使用Hibernate、JPQL和Postgres函數(shù)時(shí)出現(xiàn)意外標(biāo)記:'<Function_Name>'錯(cuò)誤的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我將PostgreSQL 9.6Hibernate 5.4.8Java 8和Spring框架一起使用。我需要調(diào)用postgres函數(shù)

CREATE OR REPLACE FUNCTION function_that_return_array(givenIds character varying(255)) RETURNS int[] AS
'
BEGIN
    RETURN string_to_array($1,'','');
END
' LANGUAGE plpgsql;

在JPQL查詢中

private static final String JPQL_QUERY =
    " SELECT NEW com.package.CustomProjection( " +
    "  e.id, " +
    "  e.value " +
    " ) " +
    " FROM SomeEntity e " +
    " WHERE e.id = ANY(function_that_return_array(:ids))";

和使用實(shí)體管理器:

@Autowired
private final EntityManager entityManager;

// ...

this.entityManager.createQuery(JPQL_QUERY, CustomProjection.class)
                .setParameter("ids", "1,2,3")
                .getResultList();

并導(dǎo)致以下異常:

antlr.NoViableAltException: unexpected token: function_that_return_array
    at org.hibernate.hql.internal.antlr.HqlBaseParser.selectFrom(HqlBaseParser.java:1055) [hibernate-core-5.4.8.Final.jar:5.4.8.Final]
    at org.hibernate.hql.internal.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:748) [hibernate-core-5.4.8.Final.jar:5.4.8.Final]
    at org.hibernate.hql.internal.antlr.HqlBaseParser.subQuery(HqlBaseParser.java:3910) [hibernate-core-5.4.8.Final.jar:5.4.8.Final]
    at org.hibernate.hql.internal.antlr.HqlBaseParser.quantifiedExpression(HqlBaseParser.java:3515) [hibernate-core-5.4.8.Final.jar:5.4.8.Final]
    at org.hibernate.hql.internal.antlr.HqlBaseParser.unaryExpression(HqlBaseParser.java:3373) [hibernate-core-5.4.8.Final.jar:5.4.8.Final]
...
antlr.MismatchedTokenException: expecting EOF, found ')'
    at antlr.Parser.match(Parser.java:211) ~[antlr-2.7.7.jar:?]
    at org.hibernate.hql.internal.antlr.HqlBaseParser.statement(HqlBaseParser.java:215) [hibernate-core-5.4.8.Final.jar:5.4.8.Final]

上面的例子非常簡單,但它正確地表示了生產(chǎn)問題。當(dāng)我在原生SQL中調(diào)用上面的函數(shù)時(shí),它工作得很好:

select *
from some_entity e 
where e.id = ANY(function_that_return_array('1,2,3,4'))

有誰知道如何在JPQL和Hibernate中調(diào)用postgres函數(shù),或者能指出我做錯(cuò)了什么?我讀了很多文章like this one,所以我試了幾十個(gè)組合,但到目前為止都沒有成功。提前謝謝。

推薦答案

在休眠方言中,您不能直接調(diào)用未注冊的自定義數(shù)據(jù)庫函數(shù)。
Clear,Hibernate中的例外是對您的函數(shù)一無所知:

unexpected token: function_that_return_array

您在此有兩個(gè)選項(xiàng):

    通過自定義函數(shù)的泛型機(jī)制調(diào)用您的函數(shù):

使用

function('function_that_return_array', '1,2,3,4')

而不是

function_that_return_array('1,2,3,4')
    第二個(gè)選項(xiàng)是注冊您的函數(shù):
    https://docs.jboss.org/hibernate/orm/5.1/javadocs/org/hibernate/dialect/Dialect.html#registerFunction-java.lang.String-org.hibernate.dialect.function.SQLFunction-

示例:

public class MyDialect extends PostgreSQLXXDialect {
    public MyDialect() {
        super();
        registerFunction("function_that_return_array", new StandardSQLFunction("function_that_return_array"));
    }
}

這篇關(guān)于使用Hibernate、JPQL和Postgres函數(shù)時(shí)出現(xiàn)意外標(biāo)記:'<Function_Name>'錯(cuò)誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,

分享到:
標(biāo)簽:Hibernate jpql Postgres 函數(shù) 意外 標(biāo)記 錯(cuò)誤
用戶無頭像

網(wǎng)友整理

注冊時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績評定2018-06-03

通用課目體育訓(xùn)練成績評定