Java JPA Postgres查询树路径
使用弹簧靴和postgres。数据库中有分层数据,路径存储在ltree列中。我试图根据路径获取特定对象,但在查询数据库时遇到问题 模型类:Java JPA Postgres查询树路径,java,hibernate,postgresql,jpa,ltree,Java,Hibernate,Postgresql,Jpa,Ltree,使用弹簧靴和postgres。数据库中有分层数据,路径存储在ltree列中。我试图根据路径获取特定对象,但在查询数据库时遇到问题 模型类: @Entity @Table(schema = "devschema", name = "family") public class Family { @Id @Column(name = "member_id") private Long memId; @Column(name = "name") privat
@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);
}
我试图将f转换为文本,但没有效果。有人知道如何解决问题吗?您选择了错误的操作员 可用的操作符,如@>,PostgreSQL文档,表示这些操作符需要gist索引。我发现在一个大数据集上速度要慢得多。(我把它拿走了。) 所以我一直在使用tilde操作符,这让我很满意。它不需要gist索引
select * from Family where familyPath ~ '*.Smith.*'
或者,如果您知道它始终是路径的终点,请留下我们的最后一个星号:
select * from Family where familyPath ~ '*.Smith'
请参见,为了更好地理解,您应该在答案中包含代码的必要部分
select * from Family where familyPath ~ '*.Smith'