I have 2 entities, Car
and Person
. One person can have multiple cars. So I have
@Entity
@NoArgsConstructor
@Getter
@Table(name="Car")
public class Car {
@Id
private String id;
private String name;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "person_id")
private Person person;
}
@Entity
@NoArgsConstructor
@Getter
@Table(name="Person")
public class Person {
@Id
private String id;
private String name;
private String address;
}
However, I only want to load cars for which the owner has an address set (so it's not null).
I have tried adding
@Where(clause = "address IS NOT NULL")
but to no help.
To translate it to SQL terms, I need the query to be
SELECT * FROM car c JOIN person p ON c.person_id = p.id WHERE p.address IS NOT NULL
CodePudding user response:
You have to use a Query
select c from Car c where c.person.address is not null
The @Where annotation (btw. this is a Hibernate proprietary and not a JPA annotation) is used on collections to filter the content.