admin管理员组文章数量:1023867
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.
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-
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
1 Answer
Reset to default 0You can add @SQLRestriction("isActive=true")
to List<B> bList
.
Note that you can't turn it off.
本文标签: javaFilter associated entity on one query with hibernate and jpaStack Overflow
版权声明:本文标题:java - Filter associated entity on one query with hibernate and jpa? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745605179a2158690.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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