【问题标题】:JPQL Select Parent entity where there is no Child entity OR Child entity has a property with specific valueJPQL 选择没有子实体的父实体或子实体具有特定值的属性
【发布时间】:2021-06-14 22:27:21
【问题描述】:

我有两个实体之间的双向@OneToMany 或父/子关系:

@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;  
}

我想选择所有没有AnswerTests Tests 并将AnswerisCorrect 属性设置为false
我正在使用Spring Boot

【问题讨论】:

    标签: java hibernate jpa spring-data-jpa jpql


    【解决方案1】:

    我假设 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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-25
      • 1970-01-01
      • 2014-09-20
      • 2020-04-13
      • 1970-01-01
      • 1970-01-01
      • 2017-05-23
      • 1970-01-01
      相关资源
      最近更新 更多