Problems with creating Spring JPA custom repository

Viewed 57

UPDATE: I have checked this page and it does not seem to be the same problem. SO's auto-closer mistakenly thought they're the same. This talks about a specific error that I am having.


I am trying to create a custom Spring Data JPA repository that adds custom behavior to all repositories. I've followed all the instructions I could find for this, but I still keep coming up with

"PropertyReferenceException: No property 'advancedSearch' found for type 'X'"

from my pom:

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.3</version>
    <relativePath />
  </parent>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
  </dependency>

Here's my code. JpqlQuery is a class I wrote. This is my interface.

@NoRepositoryBean
public interface QueryRepository<T, ID extends Serializable> extends JpaRepository<T, ID>
{
  QueryResults<T> advancedSearch(JpqlQuery<T> query) throws Exception;
}

I will be creating sub-interfaces extending that interface like so:

public interface AddressRepository extends QueryRepository<Address,Long>
{
}

This is the implementation. Note that it is the name of the interface plus "Impl" which I have read is necessary. I've tried putting this in a sub-package of the interface and also the same package as the interface, but it made no difference.

public class QueryRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID>
    implements QueryRepository<T, ID>
{
  @PersistenceContext
  private final EntityManager em;

  public QueryRepositoryImpl(Class<T> domainClass, EntityManager em)
  {
    super(domainClass, em);
    this.em = em;
  }

  public QueryRepositoryImpl(JpaEntityInformation<T, ?> entityInformation, EntityManager em)
  {
    super(entityInformation, em);
    this.em = em;
  }

  @Override
  @Transactional
  public QueryResults<T> advancedSearch(
    JpqlQuery<T> query) throws Exception
  {
...
  }
}

I also added the package name for the implementation to a ComponentScan annotation on the main application class:

@EnableEurekaClient
@SpringBootApplication
@ComponentScan({ //
                 "com.mycompany.common.repositories.impl" // For QueryRepositoryImpl
})
public class CoreRestServiceApplication
{
  ...
}

and added the following to a Configuration class in a service.

@Configuration
@EnableJpaRepositories(//
basePackages = {"com.mycompany.common.repositories.impl"}, //
repositoryBaseClass = QueryRepositoryImpl.class) // takes the place of the QueryRepositoryFactoryBean 
public class ApplicationConfiguration
{
  ...
}

The QueryRepository and QueryRepositoryImpl classes are in one jar and the services that use it are in different jars/projects. Could this be the error?

What am I missing or doing wrong?

0 Answers
Related