【问题标题】:Convert Map to HStore将 Map 转换为 HStore
【发布时间】:2019-02-08 13:47:46
【问题描述】:

我需要调用具有 hstore 参数的 Postgresql 函数。

我这样称呼它:

@Repository
public interface DocumentRepo extends JpaRepository<DokumentInstancja, Integer> {

    @Query(value = "SELECT * FROM gabinet.test(?)", nativeQuery = true)
    public Integer test(HStore mapa);
}

HStore 是我的包装类:

@Convert(converter = MyHStoreConverter.class)
public class HStore extends HashMap<String, String> {

    public HStore() {
        super();
    }

    public HStore(Map map) {
        super(map);
    }
}

我按照其他 stackoverflow 问题中的建议使用了 Converter,但它用于字段而不是类。但在我的情况下,地图不是任何其他对象的字段。

我得到的错误:

2018-09-03 15:24:17.864 TRACE 13288 --- [nio-8080-exec-3] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARBINARY] - ["key1"=>"val1","key2"=>"val2"]
2018-09-03 15:24:17.865  WARN 13288 --- [nio-8080-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 42883
2018-09-03 15:24:17.865 ERROR 13288 --- [nio-8080-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper   : ERROR: function gabinet.test(bytea) does not exist

任何人都可以建议解决我的问题吗? 提前致谢。

【问题讨论】:

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


    【解决方案1】:

    您可以使用将 hstore 参数转换为字符串

    HStoreConverter.toString(Map<?, ?> map)
    

    使用字符串更改过程参数

    procedure_name(input_string_in character varying)
    

    在过程中,您可以将其从字符串转换回 hstore

    CAST (input_string_in as hstore)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-03
      • 1970-01-01
      • 2016-07-29
      • 2013-01-06
      • 2014-02-27
      • 2015-08-06
      • 2014-05-20
      相关资源
      最近更新 更多