Wednesday, May 23, 2018

Adding own implementation in JPA

Adding your own implementation in JPA

There might be a situation where you need to use your own method based on the requirement of a application.

Spring Boot JPA provides not only CRUD Operations,if you extend JpaRepository, a whole lot of implementation will be available

Some of them are shown below

findByLastnameAndFirstname
findByLastnameOrFirstname
findByStartDateBetween
findByAgeLessThan
findByAgeLessThanEqual
findByAgeGreaterThan
findByAgeGreaterThanEqual
findByStartDateAfter
findByStartDateBefore
findByAgeIsNull
findByAge(Is)NotNull
findByFirstnameLike
findByFirstnameNotLike
findByFirstnameStartingWith


Before adding your own custom implementation,I would suggest you to look at all the implementations that are already available here.

Your own implementation can be added as shown below

public interface PersonDAO extends JpaRepository<Person, Long> {

    @Query("update User u set u.username = :username where u.userid=:userid")
    Integer updateUsernameByUserid(@Param("username") String username,@Param("userid") int userid);
}

Download Source Code


Share:

0 comments:

Post a Comment