<bdo id='GF1IW'></bdo><ul id='GF1IW'></ul>
  • <i id='GF1IW'><tr id='GF1IW'><dt id='GF1IW'><q id='GF1IW'><span id='GF1IW'><b id='GF1IW'><form id='GF1IW'><ins id='GF1IW'></ins><ul id='GF1IW'></ul><sub id='GF1IW'></sub></form><legend id='GF1IW'></legend><bdo id='GF1IW'><pre id='GF1IW'><center id='GF1IW'></center></pre></bdo></b><th id='GF1IW'></th></span></q></dt></tr></i><div id='GF1IW'><tfoot id='GF1IW'></tfoot><dl id='GF1IW'><fieldset id='GF1IW'></fieldset></dl></div>

    <tfoot id='GF1IW'></tfoot>
    <legend id='GF1IW'><style id='GF1IW'><dir id='GF1IW'><q id='GF1IW'></q></dir></style></legend>

      <small id='GF1IW'></small><noframes id='GF1IW'>

        Spring 数据存储库将 null 作为 bytea 发送到 PostgreSQL 数据库

        时间:2024-08-23
          <bdo id='Xm8Zi'></bdo><ul id='Xm8Zi'></ul>
        • <small id='Xm8Zi'></small><noframes id='Xm8Zi'>

                <legend id='Xm8Zi'><style id='Xm8Zi'><dir id='Xm8Zi'><q id='Xm8Zi'></q></dir></style></legend>

                  <tfoot id='Xm8Zi'></tfoot>
                  <i id='Xm8Zi'><tr id='Xm8Zi'><dt id='Xm8Zi'><q id='Xm8Zi'><span id='Xm8Zi'><b id='Xm8Zi'><form id='Xm8Zi'><ins id='Xm8Zi'></ins><ul id='Xm8Zi'></ul><sub id='Xm8Zi'></sub></form><legend id='Xm8Zi'></legend><bdo id='Xm8Zi'><pre id='Xm8Zi'><center id='Xm8Zi'></center></pre></bdo></b><th id='Xm8Zi'></th></span></q></dt></tr></i><div id='Xm8Zi'><tfoot id='Xm8Zi'></tfoot><dl id='Xm8Zi'><fieldset id='Xm8Zi'></fieldset></dl></div>
                    <tbody id='Xm8Zi'></tbody>

                1. 本文介绍了Spring 数据存储库将 null 作为 bytea 发送到 PostgreSQL 数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  从 MySQL 切换到 PostgreSQL 后,我发现我的 SQL 查询(spring 数据存储库接口中的 @Query)不再起作用.问题是由 null 值作为 bytea 发送的,我收到以下异常:

                  After switching from MySQL to PostgreSQL I found out that my SQL query (@Query in spring data repository interface) does not work anymore. The issue is caused by null value being sent as bytea and I'm getting following exception:

                  Caused by: org.postgresql.util.PSQLException: ERROR: operator does not exist: bigint = bytea
                  Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
                  

                  带有@Query 的存储库:

                  Repository with @Query:

                  public interface WineRepository extends PagingAndSortingRepository<Wine, Long> {
                      @Query(value = "SELECT * FROM WINE w WHERE (?1 IS NULL OR w.id = ?1)", nativeQuery = true)
                      Wine simpleTest(Long id);
                  }
                  

                  简单测试:

                  LOGGER.warn("test1: {}", wineRepository.simpleTest(1L));    //ok
                  LOGGER.warn("test2: {}", wineRepository.simpleTest(null));  //PSQLException
                  

                  在实际情况下,我有多个可以为 null 的参数,我不想在 java 代码中检查它们,而是将它们发送到 sql 查询.我在 stackoverflow 上检查了这里的问题,但没有找到一个很好的答案,尤其是对于 spring 数据存储库 @query 注释.

                  In the real case I have multiple parameters which can be null and I would prefer not checking them in java code but sending them to sql query. I have checked questions here on stackoverflow but found none with a good answer especially for spring data repository @query annotation.

                  使用 PostgreSQL 处理空值的正确方法是什么?或者您有任何提示如何解决我的方法?谢谢!

                  更新:问题似乎与 nativeQuery = true 有关,当值为 false 时,空值按预期工作.所以问题是即使启用了 nativeQuery 是否也可以使其正常工作.

                  Update: Issue seems to be related to nativeQuery = true, when value is false, null values work as expected. So the question is whether it is possible to make it function even with nativeQuery enabled.

                  推荐答案

                  试试这个.

                  SELECT *
                  FROM WINE w
                  WHERE ?1 IS NULL OR w.id = CAST(CAST(?1 AS TEXT) AS BIGINT)
                  

                  它满足类型检查器并且应该具有与原始查询相同的属性.CAST 如果发生在常量值而不是数据库行中的值上,则不会对性能造成很大影响.

                  It satisfies the type checker and should have the same properties as the original query. CAST is not a big performance hit if it happens on a constant value rather than a value from a database row.

                  这篇关于Spring 数据存储库将 null 作为 bytea 发送到 PostgreSQL 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 spring-data 中强制使用 CrudRepository 进行预加载? 下一篇:不能用Hibernate调用PostgreSQL的11个存储过程

                  相关文章

                      • <bdo id='vOHlE'></bdo><ul id='vOHlE'></ul>

                      <small id='vOHlE'></small><noframes id='vOHlE'>

                      <tfoot id='vOHlE'></tfoot>
                      <legend id='vOHlE'><style id='vOHlE'><dir id='vOHlE'><q id='vOHlE'></q></dir></style></legend>

                    1. <i id='vOHlE'><tr id='vOHlE'><dt id='vOHlE'><q id='vOHlE'><span id='vOHlE'><b id='vOHlE'><form id='vOHlE'><ins id='vOHlE'></ins><ul id='vOHlE'></ul><sub id='vOHlE'></sub></form><legend id='vOHlE'></legend><bdo id='vOHlE'><pre id='vOHlE'><center id='vOHlE'></center></pre></bdo></b><th id='vOHlE'></th></span></q></dt></tr></i><div id='vOHlE'><tfoot id='vOHlE'></tfoot><dl id='vOHlE'><fieldset id='vOHlE'></fieldset></dl></div>