使用JPA仓库从PostgreSQL序列中获取下一个值

10

我看到了类似的问题,但在我的情况下它不起作用。我需要获取序列的下一个值。

模型类:

@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();
}

我会感激任何帮助。

1个回答

18

我找到了解决方案:

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

我的错误在于我使用了 Oracle 语法,而不是我正在使用的 PostgreSQL 语法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接