【问题标题】:Getting next value from sequence with jpa repository in postgreSQL在 postgreSQL 中使用 jpa 存储库从序列中获取下一个值
【发布时间】:2018-05-09 11:13:43
【问题描述】:

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

模型类:

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

我将不胜感激。

【问题讨论】:

    标签: java postgresql jpa spring-data-jpa spring-data


    【解决方案1】:

    我找到了解决方案:

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

    我的错误是我使用了 oracle 语法而不是我正在使用的 postgreSQL。

    【讨论】:

      猜你喜欢
      • 2014-10-08
      • 1970-01-01
      • 2019-09-23
      • 2011-09-17
      • 2011-03-05
      • 2019-10-05
      • 2021-11-26
      • 2019-08-03
      • 2013-08-16
      相关资源
      最近更新 更多