【问题标题】:Postgres Ltree Extension with native query Java Entity Manager带有本机查询 Java Entity Manager 的 Postgres Ltree 扩展
【发布时间】:2021-04-28 13:30:52
【问题描述】:

我正在尝试使用本机查询从 postgres 数据库中获取树,下一个查询使用 psql 终端运行良好:

SELECT col_1 FROM my_tree WHERE parent_id  ~ lquery('*.C.*')

但是当我使用实体管理器添加相同的查询时:

private List<String> fetchTreeByParentId() {
        Query query = entityManager.createNativeQuery("SELECT col_1 FROM my_tree WHERE parent_id  ~ lquery('*.C.*')");
        return query.getResultList();
    }

我收到下一个错误:

2021-04-28 08:21:51.105  WARN 107978 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 42883
2021-04-28 08:21:51.105 ERROR 107978 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : ERROR: function lquery(unknown) does not exist
  Hint: No function matches the given name and argument types. You might need to add explicit type casts.
  Position: 65

我缺少一些东西...也许更新 postgresql jar?

项目为spring boot 2.4.2

【问题讨论】:

  • 也许您的两个会话的 search_path 设置不同?如果您的架构限定了函数怎么办?
  • 已经尝试过使用类似SELECT col_1 FROM my_schema.my_tree WHERE parent_id ~ lquery('*.C.*') 的架构,也尝试过使用强制转换SELECT col_1 FROM my_tree WHERE parent_id ~ cast('*.C.*' as lquery) ...使用强制转换的错误是数据类型lquery 不存在

标签: postgresql spring-data ltree


【解决方案1】:

感谢@jjanes,我注意到像lqueryltree 这样的数据类型在我的数据库架构中,所以我不得不将我的代码更改为:

1.- 使用 lquery 获取节点以使用 '.C.' 等模式

@Query(value = "SELECT my_col FROM {h-schema}my_tree where parent_id OPERATOR({h-schema}~) {h-schema}lquery(:parentId)", nativeQuery = true)
List<String> treeByParentId(@Param("parentId") String parentId);

2.- 使用 ltree 获取节点以使用节点的标签,例如'C'

@Query(value = "SELECT my_col from {h-schema}my_tree where parent_id OPERATOR({h-schema}<@) ( "
            + "    SELECT parent_id FROM {h-schema}my_tree WHERE node_label = :parentId "
            + "  );", nativeQuery = true)
List<String> treeByParentId(@Param("parentId") String parentId);

如果您在 public 架构中有 ltree 扩展,则无需添加 {h-schema}OPERATOR,我遇到了问题,因为我没有使用公共架构

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-27
    相关资源
    最近更新 更多