创建日期特定月份的查询[CrudRepository]

时间:2017-12-28 09:52:42

标签: spring-data spring-data-jpa

我正在尝试根据月份过滤日期。我尝试在我的CrudRepository接口中创建自定义函数但是代码没有编译。

型号:

@Entity
public class Examdates {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    @Temporal(TemporalType.DATE)
    private Date examdate;
}



@Entity
public class TestChoosenDetails {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int mid;

    @OneToOne
    private UserInfo testTaker;

    @OneToOne
    private Examdates testDate;
}

CRUDReposiotry

@Repository
public interface TestChoosenDetailsRepo extends CrudRepository<TestChoosenDetails,Integer> {
    List<TestChoosenDetails> findTestChoosenDetailsByTestDate_ExamdateMonth(int month);
}

我试过但不工作的其他方式

@Repository
public interface TestChoosenDetailsRepo extends CrudRepository<TestChoosenDetails,Integer> {
    List<TestChoosenDetails> findTestChoosenDetailsByTestDate_Examdate_Month(int month);
}

2 个答案:

答案 0 :(得分:1)

@Query("select td from TestChoosenDetails td join td.testDate ex where month(ex.examdate) = ?1")
List<TestChoosenDetails> getByExamMonth(int month);

官方doctests不包含有关使用&#39;月&#39;年'&#39;来构建回购方法的信息。但谁知道,也许这是一个无证件的特征......))

无论如何,我认为在这种情况下使用自定义查询的方法会更好。

答案 1 :(得分:0)

我认为必须

@Repository
public interface TestChoosenDetailsRepo extends CrudRepository<TestChoosenDetails,Integer> {
    List<TestChoosenDetails> findByTestDate_Examdate_Month(int month);
}