2016-02-16 1 views
1

Utilisation de Spring Boot et postgres. J'ai des données hiérarchiques dans la base de données, avec le chemin stocké dans une colonne ltree. J'essaie d'attraper un objet particulier en fonction du chemin, mais j'ai du mal à interroger la base de données.JPA Postgres requête ltree path

classe du modèle:

@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 
} 

classe de dépôt:

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); 
    } 

Un appel à partir du contrôleur, en passant dans le chemin en tant que variable de chaîne appelée chemin:

desiredMember = familyRepository.findByPath (chemin);

cède l'erreur suivante:

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 

J'ai essayé de jeter f au texte, mais en vain. Quelqu'un at-il une idée sur la façon de résoudre le problème?

+0

voir http://stackoverflow.com/questions/21447077/java-type-in- jdbc-to-postgres-ltree –

Répondre

1

Vous avez sélectionné des opérateurs incorrects.

opérateurs disponibles comme @>, < @, etc sont listés ci-dessous:

postgres ltree documentation

0

docs PostgreSQL, https://www.postgresql.org/docs/current/static/ltree.html, dit que le @> Les opérateurs ont besoin de l'indice de GIST. Et je l'ai trouvé beaucoup plus lent sur un grand ensemble de données. (Je l'ai enlevé.)

J'ai donc utilisé l'opérateur tilde à ma satisfaction. Il ne nécessite pas l'indice Gist.

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

Ou si vous le connaissez est toujours la fin du chemin, laissez notre dernier astérisque:

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

Vous devez inclure les parties nécessaires du code dans la réponse pour une meilleure compréhension – Ibo

+0

@Ibo, merci, des exemples ajoutés. – allenjom