JPA双向父子关系和存储库

时间:2018-11-19 23:04:11

标签: spring hibernate spring-data-jpa

我有一个名为test_run的表。该表具有一个名为parent_run_id的ID,该ID引用了父测试运行(我有这个ID是因为我们在自己的运行中重新运行失败的测试)。此类可根据需要工作,并在我查询ID时返回父级和子级运行。

我想找到所有没有孩子的测试。从本质上讲,这意味着我想要最新的测试重新运行,或者如果没有重新运行,则是原始测试。但是,JPA存储库生成的查询与实际应该的查询甚至不相符。

我的课(简体):

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class TestRun implements Serializable {
    private static final long serialVersionUID = 0L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long testRunId;

    @OneToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "parent_run_id", referencedColumnName = "testRunId")
    @JsonManagedReference
    private TestRun parentRun;

    @OneToOne(fetch = FetchType.LAZY, mappedBy = "parentRun")
    @JsonBackReference
    private TestRun childRun;
}

我的jpa存储库方法:

List<TestRun> getByChildRunIsNull();

它生成的查询的相关部分:

select * from "qa_result".test_run test_run 
where test_run.test_run_id is null

很明显,因为test_run_id是必填字段,所以where子句永远不会返回任何内容。当我查询所有testRun时,它们都有一个空的childRun,因此至少它以一种想要的方式(当它从数据库中读取对象时)对待我的对象。

我可以通过JPA存储库执行此操作吗?还是我必须编写自己的查询?

我不能只向父母查询这些结果,因为我们可以重新运行一次。

0 个答案:

没有答案