![](/img/trans.png)
[英]nextVal from PostgreSQL sequence in Hibernate fetch sequence multiple times
[英]Get nextval sequence value by simple java Query on a PostgreSQL DB
我正在研究PostgreSQL數據庫,我試圖通過簡單的Java查詢來恢復nextval
序列,但它不起作用:
Query q = entityManager.createQuery("SELECT nextval(numcallcartnewcart) as num");
BigDecimal result=(BigDecimal)q.getSingleResult();
return result.longValue();
(當然它不是最好的解決方案,但是我不能做得更好,因為我被Hibernate配置阻止了復合id標簽,它不接受這樣的生成器序列:
<column name="num_call" />
<generator class="sequence">
<param name="sequence">numcallcartnewcart</param>
</generator>
進入key-property標簽:
<key-property name="numCall" type="int">
<column name="num_call"/>
</key-property>
)以下是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)
它與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)
編輯:報價
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)
編輯2 :(問題是我的數據庫中沒有序列(不是好的...)
我們必須使用BigInteger,而不是BigDecimal,並使用序列名稱周圍的引號:
Query q = entityManager.createNativeQuery("SELECT nextval('numcallcartnewcart') as num");
BigInteger result=(BigInteger)q.getSingleResult();
return result.longValue();
序列的名稱必須作為字符串文字傳遞,而不是作為標識符傳遞:
entityManager.createQuery("SELECT nextval('numcallcartnewcart') as num");
手冊中的更多細節: http : //www.postgresql.org/docs/current/static/functions-sequence.html
編輯
錯誤
ERREUR:la relationship«numcallcartnewcart»n'existe pas
表示不存在名稱為numcallcartnewcart
序列。 您需要先創建序列。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.