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

偶尔善良 提交于 2020-01-13 19:56:31

问题


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

  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 :

        <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) :

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...)

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

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

回答1:


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

Edit

The error

ERREUR: la relation « numcallcartnewcart » n'existe pas

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



来源:https://stackoverflow.com/questions/29678640/get-nextval-sequence-value-by-simple-java-query-on-a-postgresql-db

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!