问题
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