Getting next value from sequence with jpa repository in postgreSQL

11,912

I've found solution:

@Query(value = "SELECT nextval('item_id_seq')", nativeQuery =
            true)
    Long getNextSeriesId();

My mistake was that I used oracle syntax instead of postgreSQL, which I am using.

Share:
11,912

Related videos on Youtube

Geha
Author by

Geha

Updated on June 04, 2022

Comments

  • Geha
    Geha almost 2 years

    I've seen the similar question, but in my situation it doesn't work. I need to get the next value of the sequence.

    Model class:

    @Entity
    @Table(name = "item")
    public class Item {
        @Id
        @SequenceGenerator(name = "id_seq", sequenceName = "item_id_seq", allocationSize = 1)
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_seq")
        @Column(name = "id", updatable = false)
        protected Long id;
    }
    

    Jpa Repository:

    public interface ItemRepository extends JpaRepository<Item, Long> {
        List<Item> findFirst10ByOrderByPublicationDateDesc();
    
        @Query(value = "SELECT item_id_seq.nextval FROM item", nativeQuery = 
        true)
        Long getNextSeriesId();
    }
    

    I would appreciate any help.