【问题标题】:How can I get a sequence number from an oracle database with jpaRepository?如何使用 jpaRepository 从 oracle 数据库中获取序列号?
【发布时间】:2019-10-31 13:37:58
【问题描述】:

我正在尝试使用 jpaRepository 从 oracle 数据库中获取序列号。

主要问题是因为我在 JpaRepository 中没有任何对象,所以我真的不知道如何解决它。序列号将是一个 Long,我只需要在我的存储库中返回这个数字。

我的代码:

@Repository
public interface InsuranceRepository extends JpaRepository<Long, Long> {

    @Query(value = "SELECT INSURANCE_VOUCHER_SEQ.nextval FROM dual", nativeQuery = true)
    Long findInsuranceVoucher();
}

我知道是错的,但我不知道我应该如何实现它。

谢谢大家。

【问题讨论】:

  • 只使用 JDBC。你为什么想要它?
  • 在没有实体的情况下,为什么要使用 Spring Data JPA?为什么还要 JPA?

标签: oracle spring-boot spring-data-jpa


【解决方案1】:

理论上你可以做到,但它会很丑。像这样使用@PersistenceContext 怎么样:

@Repository
public class InsuranceDao {

    @PersistenceContext
    private EntityManager entityManager;
    
    public Long getInsuranceVoucher() {
        Query q = entityManager.createNativeQuery("SELECT INSURANCE_VOUCHER_SEQ.nextval FROM dual");
        
        return (Long) q.getSingleResult();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-20
    • 1970-01-01
    • 1970-01-01
    • 2012-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-21
    相关资源
    最近更新 更多