首页 > 解决方案 > Spring Data JPA:findAllByOrderBy 不排序

问题描述

在我的 SpringBoot 服务中,我想从我的 mysql 数据库中获取所有“人”实体,主要按值“月”排序,其次按值“日”排序。

返回一个包含所有实体的列表,但它们的顺序是它们被添加到数据库中的顺序,独立于属性“月”和“日”。

我究竟做错了什么?

存储库:

public interface PersonRepository extends JpaRepository<Person, UUID> {
    public List<Person> findAllByOrderByMonthAscDayAsc();
}

实体:

@Entity
@Table
public class Person {

    @Id
    @GeneratedValue(generator = "uuid2")
    @GenericGenerator(name = "uuid2", strategy = "uuid2")
    @Column(columnDefinition = "BINARY(16)")
    @JsonIgnore
    private UUID id;

    @Column
    @NotNull
    @JsonProperty(access = Access.WRITE_ONLY)
    private Integer month;

    @Column
    @NotNull
    @JsonProperty(access = Access.WRITE_ONLY)
    private Integer day;

    public Person(){
    }

    public Integer getMonth() {
        return month;
    }

    public void setMonth(Integer month) {
        this.month = month;
    }

    public Integer getDay() {
        return day;
    }

    public void setDay(Integer day) {
        this.day = day;
    }
}

服务:

@Service
public class BdayService {

    @Autowired
    private PersonRepository personRepository;

    public BdayService(){
    }

    @Transactional
    public List<Person> findAllSorted(){
        List<Person> persons = personRepository.findAllByOrderByMonthAscDayAsc();
        return persons;
    }
}

标签: javamysqlsortingspring-bootspring-data-jpa

解决方案


推荐阅读