@Param不能在Spring Data JPA中工作

时间:2017-04-24 14:12:37

标签: spring postgresql spring-data-jpa kotlin

我正在设置一个Spring Data JPA Repo来处理postgresql数据库中的序列。我假设这很简单:

@Query(nativeQuery = true, value = "CREATE SEQUENCE IF NOT EXISTS ':seq_name' START WITH :startAt")
fun createSequence(@Param("seq_name") seq_name: String, @Param("startAt") startAt: Long = 0)

@Query(nativeQuery = true, value = "SELECT nextval(':seq_name')")
fun nextSerial(@Param("seq_name") seq_name: String) : Long

@Query(nativeQuery = true, value = "DROP SEQUENCE IF EXISTS ':seq_name'")
fun dropSequence(@Param("seq_name") seq_name: String)

@Query(nativeQuery = true, value = "setval(':seq_name', :set_to, false")
fun setSequence(@Param("seq_name") seq_name: String, @Param("set_to") setTo: Long)

但出于某种原因,我得到了 每当我试图调用该方法时,org.springframework.dao.InvalidDataAccessApiUsageException: Parameter with that name [seq_name] did not exist;。知道为什么会这样吗?

1 个答案:

答案 0 :(得分:2)

好的,基于@StanislavL的答案,经过一些调试后,我现在有了一个有效的解决方案。正如@posz指出我无法绑定标识符,这意味着我必须对查询进行硬编码。我将代码从JPA接口移动到一个实现的服务,这个服务不是很好但是很有效。

@Service
open class SequenceService (val entityManager: EntityManager){

    @Transactional
    fun createSequence(seq_name: String, startAt: Long = 0) {
        val query = entityManager.createNativeQuery("CREATE SEQUENCE IF NOT EXISTS ${seq_name} START ${startAt}")
        with(query){
            executeUpdate()
        }
    }

    @Transactional
    fun nextSerial(seq_name: String) : Long {
        val query = entityManager.createNativeQuery("SELECT nextval(:seq_name)")
        with(query){
            setParameter("seq_name", seq_name)
            val result = singleResult as BigInteger
            return result.toLong()
        }

    }

    @Transactional
    fun dropSequence(seq_name: String) {
        val query = entityManager.createNativeQuery("DROP SEQUENCE IF EXISTS ${seq_name}")
        with(query){
            executeUpdate()
        }
    }

    @Transactional
    fun setSequence(seq_name: String, setTo: Long){
        val query = entityManager.createNativeQuery("SELECT setval(:seq_name, :set_to, false)")
        with(query){
            setParameter("seq_name", seq_name)
            setParameter("set_to", setTo)
            singleResult
        }
    }
}

我希望当使用@SequenceGenerator不是一个选项时,这对尝试直接使用序列的下一个人有帮助。