Spring Data JPA - How to find by param with underscore

时间:2018-02-03 09:58:33

标签: spring repository spring-data-jpa

Here is my model:

public class Log {

    @Id
    private String id;
    private String project;
    private String test_no;

    // Constructor, getters and setters.
}

How can I add a findBy query which allows me to finding an element by its test_no value? I have tried those method headers but they don't work:

List<Log> findByTest_No(String test_no);

The error that my STS reports is: Invalid delivery query! No property test found in type Log!

List<Log> findByTest_no(String test_no);

The error that my STS reports is: Invalid delivery query! No property test found in type Log!

List<Log> findByTestno(String test_no);

The error that my STS reports is: Invalid derived query! No property testno found for type Log! Did you mean 'test_no'?

2 个答案:

答案 0 :(得分:3)

无需使用private String test_no;您可以使用private String testNo; Spring自动了解您在数据库中绑定test_no列。你也可以用

@Column(name = "test_no")
private String testNo;

在此更改之后,您可以执行查询,在您的问题中提到,而不需要任何自定义SQL。

List<Log> findByTestNo(String testNo);

答案 1 :(得分:2)

It seems that _ is a special character which separates properties names

... it is possible for the algorithm to select the wrong property ... To resolve this ambiguity you can use \_ inside your method name to manually define traversal points ...

Docs

So it can't find test field in Log class.

Try to use:

@Query("select log from Log log where log.test_no = ?1")
List<Log> findByTestNo(String testNo);