Spring Data方法名称中的关键字优先级

时间:2012-12-17 17:19:37

标签: java spring-data

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

1 个答案:

答案 0 :(得分:1)

  

可以通过在其中附加OrderBy子句来应用静态排序   查询方法引用属性 并提供排序方向   (Asc或Desc)。   Spring Data JPA - Reference Documentation

可能的问题:

  • “findByid ...”&lt; - ID为小写
  • 您提到要搜索“ eid ”,因此它应该是“findByEid ...”
  • 您没有指定订单方向&lt; - 这可能是主要问题;

存储库:

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实现:hibernate 4.1.9
  • Spring:3.2.0.RELEASE
  • Spring-Data:1.2.0.RELEASE

如果要创建支持动态排序的查询方法 - Spring Data JPA - 1.3.2.3. Special parameter handling