可以通过将 OrderBy 子句附加到引用属性并提供排序方向
(Asc 或 Desc)的查询方法来应用静态排序。
Spring Data JPA - 参考文档
可能的问题:
- "findByid..." <- id 是小写的
- 您提到要按“ eid ”进行搜索,因此应该是“ findByEid ...”
- 您没有指定订单方向<- 这可能是主要问题;
存储库:
public interface EmployeeRepository extends
PagingAndSortingRepository<Employee, Integer> {
public List<Employee> findByEidAndHireDateBetweenOrderByProjectIdDesc(
String eid, Date first, Date last);
}
员工实体:
@Entity
@Table(name = "employee")
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name = "employee_sequence", sequenceName = "employee_sequence", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "employee_sequence")
@Column(name = "id", unique = true, nullable = false)
private Integer id;
@Column(name = "eid", nullable = false)
private String eid;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "hire_date", nullable = false)
private Date hireDate;
@Column(name = "projectId", nullable = false)
private Integer projectId;
public Employee() {
}
// ...
// Setters / Getters
// ...
// ...
// eclipse generated hashCode / equals / toString
// ...
}
此代码适用于我的机器:
- 数据库:PostgreSQL
- ORM 实现:休眠 4.1.9
- 春天:3.2.0.RELEASE
- 弹簧数据:1.2.0.RELEASE
如果您想创建一个支持动态排序的查询方法 - Spring Data JPA - 1.3.2.3。特殊参数处理