3

For example, I have an entity with the following attribute:

@Entity
public class MyObj {
    public String nameOrId;
}

I then proceed to create the repository

public interface MyObjRepository extends JpaRepository<MyObj, String> {
    MyObj findByNameOrId(String nameOrId);
}

This fails, because it seems that Spring tokenizes the method name as findBy Name Or Id, instead of findBy nameOrId.

The resulting error is "name" property not found (or similar).

Is there a way to escape the special words ("Or", "And", etc) in the method syntax? Renaming the column to not include those words is not an option.

1
  • 1
    I'm not sure you can escape that kind of special words in your method syntax. You'll probably have to add an @Query annotation on top of your method to give the JpaRepository the query you want to execute Commented May 7, 2018 at 11:42

1 Answer 1

8

A workaround is to rename the Java field to avoid special words, while preserving the column name using the @Column annotation.

@Column(name = "nameOrId")
public String nameId;

The JPA method is renamed accordingly.

MyObj findByNameId(String nameOrId);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.