Parent class:
@Table(name = "users")
class User {
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
List<Gender> genders = new ArrayList<>();
Child class:
@Table(name = "gender")
class Gender {
@ManyToOne(cascade = {CascadeType.ALL})
@JoinColumn(name = "userId", nullable = false)
User user;
String sex;
my goal in MySQL:
SELECT * FROM users a LEFT JOIN genders b ON a.id=b.id WHERE b.sex='dasd'
Returns 1 ROW It works good. I got only one record that meets this condition.
but same code in JPQL:
SELECT a FROM Users a LEFT JOIN a.genders b WHERE b.sex ='dasd'
Returns: one user – correctly and ALL genders in table Gender, but I dont want to ALL I want only when sex ='dasd' and only one record that meets this condition.btw JPQL generateN+1 issue and for every subquery ignoring condtion sex ='dasd'
How to write subquery satisfying the condition? Can I force subquery to return only Gender matching the JPQL query?