简体   繁体   中英

Query using IN clause and JOIN are possible in JpaRepository using Spring?

When i try to list elements in a JPQL or a Native Query, my list aways return 0 elements. When I do the query directly in the database, the query is ok:

@Query(value = "SELECT xml FROM CapturaXml xml  
            JOIN xml.capturaDados dados ON dados.chnfe = xml.chnfe   
            WHERE xml.chnfe IN (:chnfes) AND dados.capturaCerts.cnpj = :cnpj")  
Page<CapturaXml> findByCnpjInChnfes(@Param("cnpj") String cnpj, @Param("chnfes") List<String> chnfes, Pageable pageable);

Or this code:

@Query(value = "SELECT xml FROM CAPTURA_XML xml  
            JOIN CAPTURA_DADOS dados ON dados.chnfe = xml.chnfe  
            WHERE dados.cnpj = :cnpj AND xml.chnfe IN (:chnfes)",  
nativeQuery = true)  Page<CapturaXml> findByCnpjInChnfes(@Param("cnpj") String cnpj, @Param("chnfes") List<String> chnfes, Pageable pageable);

My test:

@Test
    public void testBuscarPorCnpjEChnfes() {
        List<String> chnfes = Arrays.asList(CHNFE1, CHNFE2, CHNFE3, CHNFE4);

        @SuppressWarnings("deprecation")
        PageRequest page = new PageRequest(0, 10);
        Page<CapturaXml> xmls = this.capturaXmlRepository.findByCnpjInChnfes(CNPJ, chnfes, page);

        assertEquals(4, xmls.getTotalElements());
    }

My test aways list 0 elements, but when I put a List<CapturaXml> list = this.capturaXmlRepository.findAll(); the elements are inside the list.

What's the problem with my Queries?

Obs: 1: I tried to make the Fetch Eager, but not solved the problem.

I would suggest to check Spring Data version and package of used @Query. Spring Data uses its own @Query annotation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM