Dam*_*nić 5 hibernate jpa spring-data spring-data-jpa kotlin
我使用Kotlin,Spring Boot并Spring Data有Hibernate和Postgresql。
我们创建了一个存储库类,它扩展JpaRepository并发现如果提供可为空的参数,本机查询将不起作用。
在此示例中,isInternal如果我们还不知道该信息(与我们的数据库中的信息相同),则参数可以是 true、false 或 null。
    @Query(
        nativeQuery = true,
        value = "SELECT * FROM story WHERE is_internal = :isInternal",
        countQuery = "SELECT COUNT(*) FROM story WHERE is_internal = :isInternal"
    )
    fun findEmployeeStories(
        @Param("isInternal") isInternal: Boolean?,
        pageable: Pageable
    ): Page<StoryEntity>
Run Code Online (Sandbox Code Playgroud)
此查询导致:
org.postgresql.util.PSQLException: ERROR: operator does not exist: boolean = bytea
  Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
Run Code Online (Sandbox Code Playgroud)
如果我们尝试添加显式转换,错误就会改变。
    @Query(
        nativeQuery = true,
        value = "SELECT * FROM story WHERE is_internal = CAST(:isInternal AS boolean)",
        countQuery = "SELECT COUNT(*) FROM story WHERE is_internal = CAST(:isInternal AS boolean)"
    )
    fun findEmployeeStories(
        @Param("isInternal") isInternal: Boolean?,
        pageable: Pageable
    ): Page<StoryEntity>
Run Code Online (Sandbox Code Playgroud)
此查询导致:
org.postgresql.util.PSQLException: ERROR: cannot cast type bytea to boolean
Run Code Online (Sandbox Code Playgroud)
如果我们更改@Param("isInternal") isInternal: Boolean?为@Param("isInternal") isInternal: Boolean(以禁止可空值),它可以正常工作,但我们不希望那样。
以上只是重现问题的简单示例。我们的查询很复杂,这就是为什么我们尝试切换到本地查询而不是使用 JQL(在那里没有这样的问题)。
谢谢
|   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           642 次  |  
        
|   最近记录:  |