gpt4 book ai didi

java - 如何在 JPA Hibernate 中对外部列进行排序?

转载 作者:行者123 更新时间:2023-11-30 07:31:27 26 4
gpt4 key购买 nike

我有一个名为 Project 的实体,其中包含一个具有 name 字段的外部 Department 实体(见下文):

@Entity
@Table(name = "project")
public class Project
{
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid2")
@Type(type = "pg-uuid")
@Id
@Column(name = "project_id")
private UUID id;

@Column(nullable = false)
private String name;

@Column(nullable = true, unique = false)
private String description;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "department_id", nullable = false)
private Department department;

...
}

然后,在我的 ProjectService 类中,我有一个方法:

    public Page<Project> findByFilterSort(final ProjectSearchDTO dto,
ProjectSortBy sortBy,
Direction direction,
int pageNumber,
int pageSize)
{
BooleanBuilder builder = createFilter(dto);

Sort sort;
switch (sortBy)
{
case NAME:
sort = new Sort(new Order(direction, "name"),
new Order(Direction.DESC, "created"));
break;
case DEPARTMENT_NAME:
???

...
}

PageRequest request = new PageRequest(pageNumber-1, pageSize, sort);

return projectRepository.findAll(builder, request);
}

有没有办法使用这种方法按nameDepartment进行排序?如果是,怎么办?如果没有,最好的方法是什么?

提前致谢。

最佳答案

已解决:

我应该尝试一下。就像使用点 (.) 运算符引用字段一样简单。哇。

case DEPARTMENT_NAME:
sort = new Sort(new Order(direction, "department.name"),
new Order(Direction.DESC, "created"));

关于java - 如何在 JPA Hibernate 中对外部列进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36062288/

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