2

这是我被迫注释的 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 查询方法名称的命名约定?

4

1 回答 1

1

可以通过将 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。特殊参数处理

于 2012-12-19T17:50:05.153 回答