0

I am basically converting this query snippet:

I have a table A, and a table B (fk a.id), with a relationship where A can have many Bs.

I want to find A that is present in table B, which has values between X and Y.

Basically this query:

 SELECT * FROM TABELA A AS a WHERE a.id IN (SELECT b.a_id FROM TABELA B AS B WHERE b.number_residents >= 100 AND b.number_residents <= 500)

Converting to criteria builder, I have this:

    Subquery<HisCarAauuEntity> sub = cq.subquery(HisCarAauuEntity.class);
    Root<HisCarAauuEntity> subRoot = sub.from(HisCarAauuEntity.class);


predicados.add(cb.in(aauu.get("id")).value(sub.select(subRoot.get("aId")).where(cb.and((cb.greaterThanOrEqualTo(subRoot.get("nResidents"), filtro.getNcargendesde()))), cb.lessThanOrEqualTo(subRoot.get("nResidents"), filtro.getNcargenhasta()))));

This works partially, because it returns me the result A that has the filter criteria, but if A has other rows in table B (that don't match the filter) it returns me that result too...

How do I make object A return the list of object B that contains ONLY the row that matches the filter sent?

Elizama Melo
  • 103
  • 2
  • 10

0 Answers0