Spring Data JPA - How to find by param with underscore

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'?

Upvotes: 3

Views: 8296

Answers (2)

Bohdan Petrenko
Bohdan Petrenko

Reputation: 1175

There is no need to use private String test_no; You may use private String testNo; Spring automatically understand that you are binding test_no column in the database. Also you can use

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

After this changes you can execute queries, mentioned in your question without any custom sql.

List<Log> findByTestNo(String testNo);

Upvotes: 4

Adrian
Adrian

Reputation: 3134

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);

Upvotes: 4

Related Questions