通过 PostgreSQL 数据库上的简单 java 查询获取 nextval 序列值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29678640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:52:09  来源:igfitidea点击:

Get nextval sequence value by simple java Query on a PostgreSQL DB

hibernatepostgresqlhql

提问by Quentin T.

I'm working on a PostgreSQL Database and I am trying to recover a nextvalsequence by a simple Query by Java, but It's not working :

我正在处理一个 PostgreSQL 数据库,我试图nextval通过一个简单的 Java 查询来恢复一个序列,但它不起作用:

  Query q = entityManager.createQuery("SELECT nextval(numcallcartnewcart) as num");
    BigDecimal result=(BigDecimal)q.getSingleResult();   
    return result.longValue();

(Of course it's not the best solution, but I can't do better, because I'm blocked by the Hibernate configuration with the composite-id tag which don't accept a generator sequence like that :

(当然,这不是最好的解决方案,但我不能做得更好,因为我被 Hibernate 配置阻止了,它带有不接受这样的生成器序列的复合 ID 标签:

        <column name="num_call" />
             <generator class="sequence">
                    <param name="sequence">numcallcartnewcart</param>
             </generator>

into the key-property tag :

进入关键属性标签:

   <key-property name="numCall" type="int">
    <column name="num_call"/>
   </key-property>

) Here is the error of the Query:

) 这是查询的错误:

 \-[METHOD_CALL] MethodNode: '('
    +-[METHOD_NAME] IdentNode: 'nextval' {originalText=nextval}
    \-[EXPR_LIST] SqlNode: 'exprList'
       \-[IDENT] IdentNode: 'numcallcartnewcart' {originalText=numcallcartnewcart}

    at org.hibernate.hql.ast.tree.SelectClause.initializeExplicitSelectClause(SelectClause.java:154)
    at org.hibernate.hql.ast.HqlSqlWalker.useSelectClause(HqlSqlWalker.java:845)
    at org.hibernate.hql.ast.HqlSqlWalker.processQuery(HqlSqlWalker.java:633)

It's more or less the same with a createNativeQuery (but not the same error) :

它或多或少与 createNativeQuery 相同(但错误不同):

Caused by: org.postgresql.util.PSQLException: ERROR: column ? numcallcartnewcart ? does not exist
  Position: 16
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2101)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1834)

EDIT : With quote

编辑:带报价

Query q = entityManager.createNativeQuery("SELECT nextval('numcallcartnewcart') as num");

        BigDecimal result=(BigDecimal)q.getSingleResult();   
        return result.longValue();

--

——

Caused by: org.postgresql.util.PSQLException: ERREUR: la relation ? numcallcartnewcart ? n'existe pas
  Position: 16
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2101)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1834)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)

EDIT 2 : (The problem was that I don't have the sequence in my database (not in the good one...)

编辑 2 :(问题是我的数据库中没有序列(不是好的......)

And we have to use a BigInteger, not a BigDecimal, and to use quote around sequence name :

我们必须使用 BigInteger,而不是 BigDecimal,并在序列名称周围使用引号:

    Query q = entityManager.createNativeQuery("SELECT nextval('numcallcartnewcart') as num");
    BigInteger result=(BigInteger)q.getSingleResult();   
    return result.longValue(); 

回答by a_horse_with_no_name

The name of the sequence has to be passed as a string literal, not as an identifier:

序列的名称必须作为字符串文字传递,而不是作为标识符:

entityManager.createQuery("SELECT nextval('numcallcartnewcart') as num");

More details in the manual: http://www.postgresql.org/docs/current/static/functions-sequence.html

手册中的更多详细信息:http: //www.postgresql.org/docs/current/static/functions-sequence.html

Edit

编辑

The error

错误

ERREUR: la relation ? numcallcartnewcart ? n'existe pas

错误:关系?numcallcartnewcart ? 不存在的过去

indicates that no sequence with the name numcallcartnewcartexists. You need to create the sequence first.

表示不numcallcartnewcart存在具有该名称的序列。您需要先创建序列。