亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Spring JPA 查詢從序列中獲取 nextval

使用 Spring JPA 查詢從序列中獲取 nextval

牛魔王的故事 2022-07-20 16:31:34
我有一個存儲庫,我正在嘗試從序列中獲取下一個值。我需要參數化的序列名稱。存儲庫查詢類似于:@Query(value = "SELECT ?1.NEXTVAL from dual;", nativeQuery = true) String getSeqID(@Param("seqName") String seqName);但是,我得到以下異常:org.hibernate.QueryException: JPA-style positional param was not an integral ordinal    at org.hibernate.engine.query.spi.ParameterParser.parse(ParameterParser.java:187) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]    at org.hibernate.engine.query.spi.ParamLocationRecognizer.parseLocations(ParamLocationRecognizer.java:59) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]    at org.hibernate.engine.query.internal.NativeQueryInterpreterStandardImpl.getParameterMetadata(NativeQueryInterpreterStandardImpl.java:34) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]    at org.hibernate.engine.query.spi.QueryPlanCache.getSQLParameterMetadata(QueryPlanCache.java:125) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
查看完整描述

4 回答

?
Qyouu

TA貢獻1786條經驗 獲得超11個贊

package br.com.mypackage.projectname.repository;


import java.math.BigDecimal;


import org.springframework.data.jpa.repository.JpaRepository;

import org.springframework.data.jpa.repository.Query;

import org.springframework.stereotype.Repository;


@Repository

public interface MyRepository extends JpaRepository<Myentity, Long> {


    @Query(value = "SELECT SCHEMA.SEQUENCE_NAME.nextval FROM dual", nativeQuery = true)

    public BigDecimal getNextValMySequence();


}


查看完整回答
反對 回復 2022-07-20
?
ITMISS

TA貢獻1871條經驗 獲得超8個贊

在 PostgreSQL 中是:

Long value = Long.parseLong(entityManager
            .createNativeQuery("select nextval('sequence_name')")
            .getSingleResult().toString());


查看完整回答
反對 回復 2022-07-20
?
慕運維8079593

TA貢獻1876條經驗 獲得超5個贊

@Entity

@Table(name = "tabelName")

public class yourEntity{

    @Id

    @SequenceGenerator(name = "yourName", sequenceName = "yourSeqName", allocationSize = 1)

    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "yourName")

    @Column(name = "id", updatable = false)

    protected Long id;

}


@Query(value = "SELECT yourSeqName.nextval FROM tableName", nativeQuery = true)

Long getNextSeriesId();

編輯:


Query q = em.createNativeQuery("SELECT yourSeqName.NEXTVAL FROM DUAL");

return (java.math.BigDecimal)q.getSingleResult();


查看完整回答
反對 回復 2022-07-20
?
拉莫斯之舞

TA貢獻1820條經驗 獲得超10個贊

您可以使用EntityManager

entityManager.createNativeQuery("select seqname.nextval ...").getSingleResult();



查看完整回答
反對 回復 2022-07-20
  • 4 回答
  • 0 關注
  • 498 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號