java - JPA Postgres 查询ltree路径

标签 java hibernate postgresql jpa ltree

使用 Spring Boot 和 postgres。我在数据库中有分层数据,路径存储在 ltree 列中。我试图根据路径获取特定对象,但在查询数据库时遇到问题。

模型类:

@Entity
@Table(schema = "devschema", name = "family")

public class Family {

    @Id
    @Column(name = "member_id")
    private Long memId;

    @Column(name = "name")
    private String memName;

    @Column(name = "fam_path",  columnDefinition="ltree")
    private String familyPath;

    ... getters and setters
}

存储库类:

  public interface OrgRepository extends PagingAndSortingRepository <Family, Long>{

    public Family findByMemId(Long id);
    public Family findByMemName(String memName);

    @Query("select f from Family f where familyPath = ?1")
    public Family findByPath(String path);
    }

来自 Controller 的调用,将路径作为名为 path 的字符串变量传递:

desiredMember = familyRepository.findByPath(path);

产生以下错误:

2016-02-15 20:41:06.430 ERROR 88677 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : ERROR: operator does not exist: devschema.ltree = character varying
  Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
  Position: 397
2016-02-15 20:41:06.449 ERROR 88677 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception  is org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is  org.hibernate.exception.SQLGrammarException:  could not extract ResultSet] with root cause

org.postgresql.util.PSQLException: ERROR: operator does not exist: fhschema.ltree = character varying
  Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
  Position: 397

我尝试将 f 转换为文本,但无济于事。有人知道如何解决这个问题吗?

最佳答案

您选择了错误的运算符。

下面列出了 @><@ 等可用的运算符:

postgres ltree documentation

关于java - JPA Postgres 查询ltree路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35422245/

相关文章:

sql - 每个用户 ID 限制 3 行

java - String.replaceAll() 函数出错

java - 我在@OneToMany hibernate 映射中遇到错误?

java - hibernate 组织. hibernate .LazyInitializationException : failed to lazily initialize a collection of role:

java - hibernate 空间 mysql 5.7

Django/mod_wsgi/postgresql_psycopg2 : can't connect authentication fails (but settings work fine under django runserver or dbshell)

python - 使用 Pandas 在 Python 中处理大型 SQL 查询?

java - 找不到 MongoRepository (Spring Boot) 的 bean

Java泛型

java - 实例化非最终静态变量是线程安全的吗?