我得到以下数据结构
@Entity
public class Publication {
private Map<Integer, Author> authors;
// more stuff
}
@Entity
public class Author {
private String name;
// more stuff
}
我正在寻找一个查询dsl谓词,该谓词给出了我的所有出版物,其中任何Author.name包含某个字符串,例如“汉斯”
我试过了:
QPublication publication = QPublication.publication;
QAuthor author = QAuthor.author;
publication.authors.containsValue(JPAExpressions.selectFrom(author).where(author.lastName.containsIgnoreCase("Hans")));
但是,如果名称中包含“ Hans”的作者不止一个,这将引起抱怨。是否像Set一样有
publication.authors.anyValue().name.equalsIgrnoreCase("Hans")
这样的贴子? 最佳答案
我找到了一个具有联接和自定义存储库实现的解决方案:
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());
}