java - Filter associated entity on one query with hibernate and jpa? - Stack Overflow

I have question how to filter associated entity for example I have entity Apublic class A {private Lon

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
Add a comment  | 

1 Answer 1

Reset to default 0

You can add @SQLRestriction("isActive=true") to List<B> bList.

Note that you can't turn it off.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745605181a4635635.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信