2

I have been trying to create a spring boot application. In my application I would like to add some custom methods to save the data instead of using the default save method.

My application entry point is something like this:

@Configuration
@ComponentScan
@EnableJpaRepositories(repositoryImplementationPostfix = "CustomImpl")
@Import(RepositoryRestMvcConfiguration.class)
@EnableAutoConfiguration
@PropertySource("application.properties")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);         
    }

}

I have changed this line repositoryImplementationPostfix to even Impl but, it didn't work.

My CrudRepository

@RepositoryRestResource
public interface TaRepository extends CrudRepository<Ta, Integer> ,TestRepository{


    List<Ta> findByName(@Param("name") String name);

}

My Custom Repository:

public interface TestRepository {
    public void myCustomMethod(TestDto dto);
}

My Custom Repository Impl

public class TestRepositoryCustomImpl implements TestRepository{

    @PersistenceContext
    private EntityManager em;
    @Override
    public void myCustomMethod(TestDto model){
}

NOTE:

If I change my CrudRepostory from the mentioned to this:

 @RepositoryRestResource
    public interface TaRepository extends CrudRepository<Ta, Integer> {


        List<Ta> findByName(@Param("name") String name);

    }

everything works fine. But not with the custom method implementation.

Wasif Kirmani
  • 1,277
  • 3
  • 22
  • 43

1 Answers1

0

For Spring Data JPA @Repository or @RepositoryRestResource you never need to implement a Custom Interface. For any simple query you can create any kind of method, please follow the simple guide.

http://docs.spring.io/spring-data/jpa/docs/1.4.1.RELEASE/reference/html/jpa.repositories.html

For a complex query you can use JpaSpecificationExecutor.

How can I create a Predicate from a HQL query?

Community
  • 1
  • 1
Avinash
  • 4,115
  • 2
  • 22
  • 41
  • 1
    This does not cover the case where you have to pre-fetch a 2nd level field of the root entity (ex: entity.field.subfield) which is lazy for other queries. –  Apr 26 '17 at 19:17