最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin4PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far