按方法名称创建查询始终返回零计数

时间:2017-02-06 09:57:24

标签: java spring hibernate

我试图计算一个名字与给定字符串匹配的表中的学生。我想使用"包含"找到名字中包含给定字符串的学生。

我在控制器中创建了端点:

@GetMapping("/firstname")
  public int findStudentsByfirstName(@RequestParam(value="string")String firstName){
    if (firstName != null){
      System.out.println(firstName);
      return studentRepository.countByfirstNameIgnoreCase(firstName);
    } else return 0;
}

并按方法名称创建查询:

@Repository
public interface StudentRepository extends CrudRepository<Student, String> {
  int countByfirstNameIgnoreCase(String firstName);
}

实体学生中的字段firstName存在于他的超类人物中:

@Entity
@Inheritance
public abstract class Person {

  @Id
  @GeneratedValue(generator = "uuid")
  @GenericGenerator(name = "uuid", strategy = "uuid2")
  @Column(name = "uuid", unique = true)
  @JsonProperty("id")
  protected String id;

  @Column
  @NotNull
  @JsonProperty("lastName")
  protected String lastName;

  @Column
  @NotNull
  @JsonProperty("firstName")
  protected String firstName;

当我要求localhost:8080 /学生时,我得到:

{"id":"c30e24a8-6b4a-4eec-a28c-afec74709141",
"lastName":"Schlund",
"firstName":"Rainer",
"eMail":"max@student.kit.edu",
"phoneNumber":"011981954856821",
"street":"Musterstraße 1",
"place":"123456 Musterstadt",
"role":"Student",
"immatriculationNumber":"196495619562",
"major":"Informatik",
"semester":27,
"spoId":"SPO08"}

所以有一个人的名字是&#34; Rainer&#34;

但是当我尝试localhost:8080 / student / firstname?string =&#34; Rainer&#34;结果是0。

1 个答案:

答案 0 :(得分:-1)

确定正确的方法是

int countByfirstNameIgnoreCase(String firstName);

而不是

int findByfirstNameIgnoreCase(String firstName);

...