Java jdbctemplate postgres json

Java jdbctemplate postgres json,java,postgresql,jdbctemplate,jsonb,Java,Postgresql,Jdbctemplate,Jsonb,嗨,请告诉我我能做什么? 我正在使用postgres,表中有两个字段: -id(bigint) -属性(jsonb) 属性结构={id:“…”,名称:“…”} 我需要检查json中是否有具有相同id的对象 所以我有一个方法: `public Optional<Long> getIdByAtlasId(Long atlasId) { String sql = "SELECT id FROM dictionary WHERE attributes ->

嗨,请告诉我我能做什么? 我正在使用postgres,表中有两个字段:

  • -id(bigint)

  • -属性(jsonb)

属性结构={id:“…”,名称:“…”}


我需要检查json中是否有具有相同id的对象 所以我有一个方法:

`public Optional<Long> getIdByAtlasId(Long atlasId) {
            String sql = "SELECT id FROM dictionary WHERE attributes -> 'attributes' ->> 'id' = :atlas_id";
            try {
                return Optional.ofNullable(jdbcTemplate.queryForObject(sql, Collections.singletonMap("atlasId", atlasId),Long.class));
            } catch (Exception e) {
                return Optional.empty();
            }
        }`
`public可选getIdByAtlasId(长atlasId){
String sql=“从字典中选择id,其中属性->'attributes'->'id'=:atlas_id”;
试一试{
返回可选的.ofNullable(jdbcTemplate.queryForObject(sql,Collections.singletonMap(“atlasId”,atlasId),Long.class));
}捕获(例外e){
返回可选的.empty();
}
}`
它总是返回null,因为我在表中有这个ID,所以首先我决定在db中尝试这个查询:
从字典中选择id,其中属性->'id'=85

并给出“运算符不存在:text=integer”。 下一次尝试是:

从属性所在的字典中选择id->'id'='85'

然后返回id 在本例中,我决定将字符串参数传递给我的方法:

jdbcTemplate.queryForObject(sql,Collections.singletonMap(“atlasId”,atlasId.toString()),Long.class)

但它仍然返回null

提前感谢您,感谢您的帮助