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();
添加回答
举报