本文介绍了在JPA中过滤子实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
无论如何,有没有用jpa编写查询来过滤子实体?
Is there anyway to write a query in jpa that filters child entities?
我有这个实体:
@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属性上进行过滤.
Now I would like to get the project and his flow but the steps in the flow should be filtered on a start and endtime property.
我现在正在做的是找到项目,然后遍历所有步骤并过滤步骤.
What I'm doing right now is find the project and then loop trough all the steps and filter the steps.
赞:
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);
}
}
步骤实体:
@Entity
public class Step {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
private LocalDateTime startTime;
private LocalDateTime endTime;
}
我看不到如何在项目存储库中添加查询,该项目存储库是用于过滤子实体中步骤的原始存储库.
I don't see how I can add a query in my project repository which is a crudrepository to filter the steps in a child entity.
推荐答案
存储库中的此类功能就足够了.
Function like this in your Repository should be enough.
findByFlow_Steps_StartTimeAfterAndFlow_Steps_EndTimeBefore(Date startTime, Date endTime);
这篇关于在JPA中过滤子实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!