2017-02-06 6 views
0

私は与えられた文字列にファーストネームが一致するテーブルで生徒を数えようとしています。 Lateron私は、 "contains"を使って名前に与えられた文字列が含まれている学生を探したいと思っています。メソッド名によるクエリの作成では常にゼロカウントが返されます

@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/studentを求めているときは、

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

だから、最初の名前 "ライナー" である人がある

しかし、私はlocalhostを試してください:?8080 /学生/ FIRSTNAMEを文字列= "ライナー" の結果は0

+0

このクエリを実行するメソッドは表示されていません。おそらくバグがあります。 –

+0

このコード行からHibernateによって自動的に作成されます。 "int countByfirstNameIgnoreCase(String firstName);"上記の2番目のコードブロックに示されています。 –

+1

二重引用符を使わずに試しましたか? localhost:8080/student/firstname?string = Rainer – RubioRic

答えて

-1

確かです右の方法は

int countByfirstNameIgnoreCase(String firstName); 

なく

int findByfirstNameIgnoreCase(String firstName); 
であること

??

+0

私はSpring-Data 1.7.1の時点で、 'countBy'が当てはまります。 – Naros

関連する問題