I need to write a search query on multiple tables in database in spring boot web application.
It uses spring data jpa. I know we can write native query in spring da
You need to do a CustomRepository
and add a method with native query.
I do that this way:
Create your custom repository:
public interface OcorrenciaRepositoryCustom {
List
Implement your custom repository: (The name of the implemantation must be the name of original repository add Impl as suffix.)
public class OcorrenciaRepositoryImpl implements OcorrenciaRepositoryCustom {
@PersistenceContext
private EntityManager entityManager;
@Override
public List
Extend your custom repository from main repository:
public interface OcorrenciaRepository extends JpaRepository, OcorrenciaRepositoryCustom {
Ocorrencia findByPosto(Posto posto);
}
Now, in the service, you can call your new method from the main repository.
@Autowired
private OcorrenciaRepository repository;
public List
It is important that the custom repository is under the package searched by JpaRepositories
@EnableJpaRepositories("com.test.my.repository")
I used Spring-Data-Jpa 1.9 in this example. It worked perfectly in my project.