在Spring数据中使用带有方法参数的Before关键字

时间:2017-08-07 12:25:45

标签: java spring spring-data

我有包含字段日期publicationDate和布尔档案的实体组合。我想在date作为参数传递并将archival标志设置为false之前获取具有publicationDate的Compositions列表。在完成了一些'从方法名称'教程和文档中创建查询后,我提出了方法

@Projection(types = SuperClass.class)
public interface SuperClassProjection {
  Long getId();
}

List<Composition> findByPublicationDateBeforeDateAndArchivalFalse(Date date);

但这一切都不起作用。两者都使用

给出UnsatisfiedDependencyException
List<Composition> findByPublicationDateBeforeDateAndArchivalFalse(@Param("date")Date date);

Intellij还强调了BeforeDate,因为它无法解析beforeDate之前的属性。使用带参数的关键字的正确方法是什么,因此可以通过Spring将参数与字段区分开来?

1 个答案:

答案 0 :(得分:1)

将您的方法重构为以下内容:

List<Composition> findByPublicationDateBeforeAndArchivalFalse(Date date);

Before关键字会将publicationDate与您传递的date作为参数进行比较,因此无需说BeforeDate。就像Spring Data documentation中的一个例子:

Before - &gt; findByStartDateBefore生成以下SQL部分… where x.startDate < ?1