使用postgreSQL中的jpa存储库从序列中获取下一个值

Geh*_*eha 4 java jpa spring-data spring-data-jpa

我见过类似的问题,但在我的情况下它不起作用.我需要获得序列的下一个值.

型号类:

@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;
}
Run Code Online (Sandbox Code Playgroud)

Jpa存储库:

public interface ItemRepository extends JpaRepository<Item, Long> {
    List<Item> findFirst10ByOrderByPublicationDateDesc();

    @Query(value = "SELECT item_id_seq.nextval FROM item", nativeQuery = 
    true)
    Long getNextSeriesId();
}
Run Code Online (Sandbox Code Playgroud)

我将不胜感激任何帮助.

Geh*_*eha 9

我找到了解决方案:

@Query(value = "SELECT nextval('item_id_seq')", nativeQuery =
            true)
    Long getNextSeriesId();
Run Code Online (Sandbox Code Playgroud)

我的错误是我使用oracle语法而不是postgreSQL,我正在使用它.