Spring findAll by parent property

Spring findAll by parent propriety

我正在使用 JHipster 生成应用程序。我有这张地图:Arquivo 与 Tabela 有 OneToMany 关系。并且 Tabela 与 Campo 具有 OneToMany 关系。

Arquivo 有一个名为 "versao"(字符串)的 属性。我想做类似的事情:

Page<Campo> findAllByArquivoVersao(String versao, Pageable pageable);

但是我收到这个错误:

Failed to create query for method public abstract org.springframework.data.domain.Page br.com.app.repository.CampoRepository.findAllByArquivoVersao(java.lang.String,org.springframework.data.domain.Pageable)! No property arquivoVersao found for type Campo!

我可以执行类似 findAllByTabelaId 的操作...所以我的映射没问题。如何通过我的 parent 的 parent 的 属性 进行查询过滤?

选项 1

ArquivoRepository 找到你的 Arquivo by Versao,然后为每个人调用 arquivo.getTabelas()getCampos() 以获得你想要的 campos 集合。

选项 2

CampoRepository中定义你自己的@Query,像这样:

@Query("select c from Campo c where c.tabela.arquivo.versao = :versao")
Page<Campo> findAllByArquivoVersao(@Param("versao") String versao, Pageable pageable);

如果你想 Campos 通过部分比较他们的 versao 你可以这样做:

@Query("select c from Campo c where upper(c.tabela.arquivo.versao) like concat('%',upper(:versao),'%')")
Page<Campo> findAllByArquivoVersao(@Param("versao") String versao, Pageable pageable);

在不知道您是如何设计您的实体(获取、可空性等)的情况下,这就是我所能猜到的。

我没有测试任何这段代码。

我明白了。 Spring docs 项目 4.4.3 指定了这种情况:

我必须这样命名我的接口方法:findByTabelaArquivoVersao