JPA Postgres查询ltree路径

时间:2016-02-16 01:46:01

标签: 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);
    }

来自控制器的调用,将路径作为字符串变量传递,名为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转换为文本,但无济于事。任何人都知道如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

您选择了错误的操作符。

@&gt; &lt; @ 等可用的运算符列在下面:

  

postgres ltree documentation

答案 1 :(得分:0)

PostgreSQL文档https://www.postgresql.org/docs/current/static/ltree.html表示@&gt;运营商需要gist指数。我发现它在大型数据集上显着变慢。 (我把它删除了。)

所以我一直在使用代字号运算符让我满意。它并不需要gist索引。

select * from Family where familyPath ~ '*.Smith.*'

或者如果你知道它始终是路径的终点,请留下我们的最后一个星号:

select * from Family where familyPath ~ '*.Smith'
相关问题