这是我不得不注释的JPQL:
@Query(
"SELECT a from Employee a WHERE eid = :eid and hireDate between :first and :last order by projectId"
)
因为我无法让它工作:
public List<Employee>
findByEidAndHireDateBetweenOrderByProjectId(
String eid, Date first, Date last);
我的方法名是否正确遵循Spring Data查询方法名称的命名约定?
答案 0 :(得分:1)
可以通过在其中附加OrderBy子句来应用静态排序 查询方法引用属性 并提供排序方向 (Asc或Desc)。 Spring Data JPA - Reference Documentation
可能的问题:
存储库:
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
// ...
}
此代码适用于我的机器:
如果要创建支持动态排序的查询方法 - Spring Data JPA - 1.3.2.3. Special parameter handling