在PostgreSQL中使用JPA存储库从序列中获取下一个值 [英] Getting next value from sequence with jpa repository in postgreSQL
本文介绍了在PostgreSQL中使用JPA存储库从序列中获取下一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经看到了类似的问题,但是在我的情况下,它是行不通的. 我需要获取序列的下一个值.
I've seen the similar question, but in my situation it doesn't work. I need to get the next value of the sequence.
模型类:
@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存储库:
public interface ItemRepository extends JpaRepository<Item, Long> {
List<Item> findFirst10ByOrderByPublicationDateDesc();
@Query(value = "SELECT item_id_seq.nextval FROM item", nativeQuery =
true)
Long getNextSeriesId();
}
我将不胜感激.
推荐答案
我找到了解决方法:
@Query(value = "SELECT nextval('item_id_seq')", nativeQuery =
true)
Long getNextSeriesId();
我的错误是我使用的是oracle语法而不是PostgreSQL.
My mistake was that I used oracle syntax instead of postgreSQL, which I am using.
这篇关于在PostgreSQL中使用JPA存储库从序列中获取下一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文