I have question how to filter associated entity for example I have entity A
public class A {
private Long id;
@OneToMany
private List<B> bList;
}
and entity B:
public class B {
private Long id;
private Boolean isActive;
@ManyToOne
private A a;
}
And for example I want to get List of A entities with filtered fetched B entities with isActive = true. But the problem is, that I want to do it in one query without foreach loop and searching separately B entities. I heared about @FilterJoinTable but this invoke another query to search B entity even if B entity is fetched eagerly. Also I try criteria api with listJoin an use .on() function but this doesn't work. So maybe there is no solution to get A entity with filtered B entity in one query and I should use only stream filter?
I have question how to filter associated entity for example I have entity A
public class A {
private Long id;
@OneToMany
private List<B> bList;
}
and entity B:
public class B {
private Long id;
private Boolean isActive;
@ManyToOne
private A a;
}
And for example I want to get List of A entities with filtered fetched B entities with isActive = true. But the problem is, that I want to do it in one query without foreach loop and searching separately B entities. I heared about @FilterJoinTable but this invoke another query to search B entity even if B entity is fetched eagerly. Also I try criteria api with listJoin an use .on() function but this doesn't work. So maybe there is no solution to get A entity with filtered B entity in one query and I should use only stream filter?
Share Improve this question edited Nov 18, 2024 at 17:40 Pioter asked Nov 18, 2024 at 17:25 PioterPioter 133 bronze badges 1 |1 Answer
Reset to default 0You can add @SQLRestriction("isActive=true")
to List<B> bList
.
Note that you can't turn it off.
from A join fetch bList b where b.isActive
: translete this query to criteria is not so difficult (is just a join-fetch with a clause) – Luca Basso Ricci Commented Nov 19, 2024 at 6:55