gpt4 book ai didi

java - @ManyToOne 的 JPA 规范 CriteriaBuilder

转载 作者:行者123 更新时间:2023-12-01 09:45:43 26 4
gpt4 key购买 nike

我正在尝试执行一些过滤,但通过 ManyToOne 进行过滤时遇到一些问题,每次我获取结果集时,我都会获取所有 Task 对象,无论它们所链接的项目是什么。我有以下实体:

@Entity
public class Project implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "project_seq_gen")
@SequenceGenerator(name = "project_seq_gen", sequenceName = "project_id_seq", allocationSize = 1)
private Long id;

@Column(unique = true)
private String projectId;

//more things
}

任务类通过其 id 与项目链接

@Entity
public class Task implements Serializable {

@Id
private String id;

@Column
private String name;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "project_tasks")
private Project project;

@Column
private Enum<TaskType> type;

//more things....
}

在任务表中,它看起来像这样

 |id      |project_tasks | 
|--------|--------------|
| TASK1 | 1 |
| TASK2 | 2 |
|--------|--------------|

我有 jparepository 来搜索任务:

@Repository
public interface TaskRepository extends JpaRepository<Task, Integer> , JpaSpecificationExecutor {

以及使用 org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor 生成的元模型的规范

 public class TaskSpecification implements Specification<Task> {
private final TaskFilter taskFilter;

public TaskSpecification(TaskFilter task) {
this.taskFilter = task;
}

@Override
public Predicate toPredicate(Root<Task> task, CriteriaQuery<?> cq, CriteriaBuilder cb) {
Predicate predicate = cb.conjunction();
//project
//approach 1
cb.and(predicate, task.join(Task_.project).get(Project_.id).in(taskFilter.getProject().getId()));
//approach 2
cb.and(predicate, cb.equal(task.get(Task_.project).get(Project_.id), taskFilter.getProject().getId()));
//some other filters to follow
if (StringUtils.isNotBlank(taskFilter.getByState())) {
if (TaskFilter.OPEN.equals(taskFilter.getByState())) {
predicate = cb.and(predicate, task.get(Task_.state).in(TaskState.TO_DO, TaskState.ONGOING, TaskState.COMPLETE, TaskState.BLOCKED));
} else {
predicate = cb.and(predicate, task.get(Task_.state).in(TaskState.valueOf(taskFilter.getByState())));
}
}
return predicate;
}
}

服务电话:

public List<Task> findBySpecification(TaskFilter filter) {
return taskRepo.findAll(new TaskSpecification(filter));
}

不幸的是,使用方法 1 和方法 2 的每个查询都会返回所有任务,无论项目 ID 是什么。我还启用了 hibernate.show_sql ,控制台上显示了以下内容

方法1

Hibernate: select task0_.id as id1_12_, task0_.active as active2_12_, task0_.task_assignee as task_as21_12_, task0_.create_date as create_d3_12_, task0_.description as descript4_12_, task0_.due_date as due_date5_12_, task0_.estimate as estimate6_12_, task0_.estimated as estimate7_12_, task0_.finishDate as finishDa8_12_, task0_.inSprint as inSprint9_12_, task0_.lastUpdate as lastUpd10_12_, task0_.loggedWork as loggedW11_12_, task0_.name as name12_12_, task0_.task_owner as task_ow22_12_, task0_.parent as parent13_12_, task0_.priority as priorit14_12_, task0_.project_tasks as project23_12_, task0_.release_id as release24_12_, task0_.remaining as remaini15_12_, task0_.state as state16_12_, task0_.story_points as story_p17_12_, task0_.subtasks as subtask18_12_, task0_.task_order as task_or19_12_, task0_.type as type20_12_ from Task task0_ inner join Project project1_ on task0_.project_tasks=project1_.id where 1=1 and (task0_.state in (? , ? , ? , ?))

方法2

 Hibernate: select task0_.id as id1_12_, task0_.active as active2_12_, task0_.task_assignee as task_as21_12_, task0_.create_date as create_d3_12_, task0_.description as descript4_12_, task0_.due_date as due_date5_12_, task0_.estimate as estimate6_12_, task0_.estimated as estimate7_12_, task0_.finishDate as finishDa8_12_, task0_.inSprint as inSprint9_12_, task0_.lastUpdate as lastUpd10_12_, task0_.loggedWork as loggedW11_12_, task0_.name as name12_12_, task0_.task_owner as task_ow22_12_, task0_.parent as parent13_12_, task0_.priority as priorit14_12_, task0_.project_tasks as project23_12_, task0_.release_id as release24_12_, task0_.remaining as remaini15_12_, task0_.state as state16_12_, task0_.story_points as story_p17_12_, task0_.subtasks as subtask18_12_, task0_.task_order as task_or19_12_, task0_.type as type20_12_ from Task task0_ where 1=1 and (task0_.state in (? , ? , ? , ?))

非常乐意提供帮助。最好的问候

最佳答案

您可能无法正确使用 cb.and() 函数。此函数将返回一个新的组合谓词,而不是更改当前的谓词。
所以而不是
cb.and(predicate, task.join(Task_.project).get(Project_.id).in(taskFilter.getProject().getId()));
你应该有
predicate = cb.and(predicate, task.join(Task_.project).get(Project_.id).in(taskFilter.getProject().getId()));

关于java - @ManyToOne 的 JPA 规范 CriteriaBuilder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38056066/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com