Java 在postgreSQL中使用jpa存储库从序列中获取下一个值
我见过类似的问题,但在我的情况下,它不起作用。 我需要得到序列的下一个值 模型类:Java 在postgreSQL中使用jpa存储库从序列中获取下一个值,java,postgresql,jpa,spring-data-jpa,spring-data,Java,Postgresql,Jpa,Spring Data Jpa,Spring Data,我见过类似的问题,但在我的情况下,它不起作用。 我需要得到序列的下一个值 模型类: @Entity @Table(name = "item") public class Item { @Id @SequenceGenerator(name = "id_seq", sequenceName = "item_id_seq", allocationSize = 1) @GeneratedValue(strategy = GenerationType.SEQUENCE, gene
@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();
}
public interface ItemRepository扩展了JpaRepository{
按OrderByPublicationDateDesc()列出FindFirst10;
@查询(value=“SELECT item\u id\u seq.nextval FROM item”,nativeQuery=
(对)
Long getNextSeriesId();
}
非常感谢您的帮助。我找到了解决方案:
@Query(value = "SELECT nextval('item_id_seq')", nativeQuery =
true)
Long getNextSeriesId();
我的错误是我使用了oracle语法,而不是我正在使用的postgreSQL