JPQL Select 没有子实体的父实体或子实体具有具有特定值的 属性
JPQL Select Parent entity where there is no Child entity OR Child entity has a property with specific value
我在两个实体之间存在双向 @OneToMany
或 parent/child 关系:
@Entity
public class Test {
@OneToMany(mappedBy = "test")
private Set<Answer> answers = new HashSet<>();
}
@Entity
public class Answer {
@ManyToOne
@JoinColumn(name = "test_id")
private Test test;
@Column
private Boolean isCorrect;
}
我想 select 所有 Test
没有 Answer
或 Test
有 Answer
的 isCorrect
属性 设置为 false
.
我正在使用 Spring Boot
.
我假设test的Id是Integer类型:
public interface TestRepository extends JpaRepository<Test, Integer> {
@Query("select t from Test t join t.answers a where a is empty or a.isCorrect = true")
List<Test> findTestsWithNoOrIncorrectAnswers();
}
我在两个实体之间存在双向 @OneToMany
或 parent/child 关系:
@Entity
public class Test {
@OneToMany(mappedBy = "test")
private Set<Answer> answers = new HashSet<>();
}
@Entity
public class Answer {
@ManyToOne
@JoinColumn(name = "test_id")
private Test test;
@Column
private Boolean isCorrect;
}
我想 select 所有 Test
没有 Answer
或 Test
有 Answer
的 isCorrect
属性 设置为 false
.
我正在使用 Spring Boot
.
我假设test的Id是Integer类型:
public interface TestRepository extends JpaRepository<Test, Integer> {
@Query("select t from Test t join t.answers a where a is empty or a.isCorrect = true")
List<Test> findTestsWithNoOrIncorrectAnswers();
}