有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

jpa中的java过滤器子实体

在jpa中是否有编写过滤子实体的查询的方法

我有这个实体:

@Entity
public class Project {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @ManyToOne(targetEntity =  Flow.class)
    private Flow flow;
}

流实体:

@Entity
public class Flow {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @OneToMany(targetEntity = Step.class)
    private List<Step> steps;
}

现在我想得到项目和他的流程,但是流程中的步骤应该在start和endtime属性上进行过滤

我现在做的是找到项目,然后循环所有步骤并过滤步骤

像这样:

List<Step> steps = project.getFlow().getSteps();
List<Step> filteredSteps = new ArrayList<>();
for (int i = 0; i < steps.size(); i++) {
    Step step = steps.get(i);

    if (step.getStartTime() == null || step.getEndTime() == null) {
        break;
    }

    if (step.getStartTime().isAfter(start) && step.getEndTime().isBefore(end)) {
        filteredSteps.add(step);
    }
}

Step实体:

@Entity
public class Step {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    private String name;

    private LocalDateTime startTime;
    private LocalDateTime endTime;
}

我不知道如何在我的项目存储库中添加一个查询,它是一个用于过滤子实体中的步骤的crudepository


共 (1) 个答案

  1. # 1 楼答案

    在存储库中这样的功能应该足够了

    findByFlow_Steps_StartTimeAfterAndFlow_Steps_EndTimeBefore(Date startTime, Date endTime);