Lucas300
Lucas300

Reputation: 99

Use Entity fields access with m prefixed fields but still use eg findByName instead of findByMName in spring-data

I have JPA entities with fields like String mName. When using Spring data repositories I want to use e.g findByName instead of findByMName. I know that I can set the entity access to on properties instead of fields but that has other drawbacks.

Upvotes: 0

Views: 29

Answers (2)

dasunse
dasunse

Reputation: 3089

Use this way in your repository

@Query("SELECT t FROM Todo t where t.mName = ?1")
Todo findByName(String mName);

Upvotes: 2

sovannarith cheav
sovannarith cheav

Reputation: 793

You can create default method in Interface Repository like this :

@Repository
public interface TodoRepository extends CrudRepository<Todo, Integer> {

    default Optional<Todo> findByName(String mName){
        return findByMName(mName);
    }

}

Then you can call repo.findByName(mName).

Upvotes: 1

Related Questions