4 回答
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();
}
TA貢獻1871條經驗 獲得超8個贊
在 PostgreSQL 中是:
Long value = Long.parseLong(entityManager
.createNativeQuery("select nextval('sequence_name')")
.getSingleResult().toString());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();
TA貢獻1820條經驗 獲得超10個贊
您可以使用EntityManager:
entityManager.createNativeQuery("select seqname.nextval ...").getSingleResult();添加回答
舉報
