14

I have a repository with which I am trying to get next value from sequence. The sequence name I need to be parameterized.

The repository query looks a like:

@Query(value = "SELECT ?1.NEXTVAL from dual;", nativeQuery = true) String getSeqID(@Param("seqName") String seqName);

But, I am getting following exception:

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]
Andronicus
  • 25,419
  • 17
  • 47
  • 88
Ashish Chauhan
  • 486
  • 2
  • 8
  • 22

5 Answers5

13
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();

}
Hinotori
  • 552
  • 6
  • 15
  • 3
    Native query similar to this worked for me ```select nextval('sequence_name')``` – Nitin Kamate Apr 19 '21 at 03:36
  • 1
    is there a way to get sequence number without using native query? – Venkata Ramireddy CH Jan 27 '22 at 18:51
  • How can I use a dynamic SCHEMA for the native query (different than the default schema). For example in the case of an Oracle DB where someone wants to select a sequence of a different schema which might be dynamic. – Mts Sep 01 '22 at 10:51
  • What would be `Myentity` for the repository if the sequence is not linked to a table? – Mauricio Toledo Jan 16 '23 at 22:49
  • @MauricioToledo you can't . In the Id of MyEntity, you should specify the sequence. And the entity is a table. If you are using Oracle, maybe should find something in Spring JPA that can access this sequence – Hinotori Jan 17 '23 at 00:03
  • 1
    @MauricioToledo thinking again, maybe you can use the sequence, creating another method in some repository, but you must change the schema.sequence_name that matches the one that you want to use... i don't know, never did this, maybe works – Hinotori Jan 17 '23 at 00:15
  • 1
    @Hinotori I set the sequence as an SQL Native query in another repo. And, it did work! – Mauricio Toledo Jan 24 '23 at 19:21
5

In PostgreSQL it is:

Long value = Long.parseLong(entityManager
            .createNativeQuery("select nextval('sequence_name')")
            .getSingleResult().toString());
Ondrej Sotolar
  • 1,352
  • 1
  • 19
  • 29
4
@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();

EDIT:

Query q = em.createNativeQuery("SELECT yourSeqName.NEXTVAL FROM DUAL");
return (java.math.BigDecimal)q.getSingleResult();
Romil Patel
  • 12,879
  • 7
  • 47
  • 76
2

You can use EntityManager:

entityManager.createNativeQuery("select seqname.nextval ...").getSingleResult();
Andronicus
  • 25,419
  • 17
  • 47
  • 88
  • The database I am using is oracle and the entity manager has been configured in another config class as a bean. The nextval(seqname) is giving error says unknown value. Can't I used anything with above approach within JPA repository? – Ashish Chauhan Mar 05 '19 at 15:48
  • I've edited the question with oracle dialect, I'm not sure, you can try putting it in `@Query` – Andronicus Mar 05 '19 at 17:55
1

I fixed this issue using the following code :

@Query(value = "SELECT NEXT VALUE FOR code_sequence", nativeQuery = true)
public BigDecimal getNextValCodeSequence();

NB: you should replace code_sequence with your sequence name

Ousama
  • 2,176
  • 3
  • 21
  • 26