2017-07-17 3 views
0

bekam ich die folgende DatenstrukturAbfrage Kartenwert mit SpringData und QueryDSL-JPA

@Entity 
public class Publication { 
    private Map<Integer, Author> authors; 
    // more stuff 
} 

@Entity 
public class Author { 
    private String name; 
    // more stuff 
} 

Ich bin für eine Abfrage dsl Prädikat suchen, die meine ganze Publikation gibt, wo jeder Author.name zum Beispiel eine bestimmte Zeichenfolge enthält „Hans“

Ich habe versucht:

QPublication publication = QPublication.publication; 
QAuthor author = QAuthor.author; 
publication.authors.containsValue(JPAExpressions.selectFrom(author).where(author.lastName.containsIgnoreCase("Hans"))); 

Aber dies beklagt, wenn es mehr als ein Autor ist „Hans“ im Namen enthält. Gibt es einen Moment wie publication.authors.anyValue().name.equalsIgrnoreCase("Hans") wie es für ein Set ist?

Antwort

0

Ich glaube, Sie die BooleanBuilder

in Ihrem Repository verpasst haben - versuchen diese

QPublication publication = QPublication.publication; 
QAuthor author = QAuthor.author; 
BooleanBuilder where= new BooleanBuilder(); 
where.and(publication.author.name.containsIgnoreCase(textToSearch)); 

//pass the where condition to the desired Repo method 
repo.findAll(where); 
+0

Seit ' Publication.authors' ist eine Map, es gibt keine 'publication.author' Der' BoolenBuilder' wird nicht benötigt, wenn Sie nur eine Bedingung haben – dve

1

fand ich eine Lösung mit einem Join und eine eigene Repository-Implementierung:

public class PublicationRepositoryImpl extends QueryDslRepositorySupport 
    implements PublicationRepositoryCustom { 

@Override 
public Page<Publication> findByAnyAuthorName(String name, PageRequest pageRequest) { 
    QPublication publication = QPublication.publication; 
    JPQLQuery<Publication> query = from(publication); 
    QAuthor author = QAuthor.author; 
    query.join(publication.authors, author); 
    query.where(author.name.containsIgnoreCase(name); 
    JPQLQuery<Publication> pagedQuery = getQuerydsl() 
     .applyPagination(pageRequest, query); 

    return PageableExecutionUtils.getPage(pagedQuery.fetch(), pageRequest, 
    () -> query.fetchCount()); 
} 
Verwandte Themen