1

I am trying to use customRepository with CRUDRepository. Getting below error:

ConfigRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract java.lang.Integer

Caused by: java.lang.IllegalArgumentException: Failed to create query for method public abstract java.lang.Integer IConfigCustomRepository.refreshTable(java.lang.String,long)! At least 2 parameter(s) provided but only 1 parameter(s) present in query.

At least 2 parameter(s) provided but only 1 parameter(s) present in query.

In my IConfigCustomRepository, I have one method, in which I am passing 3 parameters, 2 param to set parameters of query and one more is String which is to query that needs to be executed.

Interface --- IConfigCustomRepository.Java

public interface IConfigCustomRepository {

    Integer refreshTable(String queryText, long lastRefreshTime,  long currentTime);

}
public class ConfigCustomRepositoryImpl implements IConfigCustomRepository {

    @PersistenceContext
    EntityManager entityManager;

    @Override
    public Integer refreshTable(String queryText, long lastRefreshTime,  long currentTime) {
        return entityManager.createNativeQuery(queryText)
                .setParameter(1, lastRefreshTime)
                .setParameter(2, currentTime)
                .executeUpdate();
    }

}

And I have extended it in ConfigRepository

@Repository
@Transactional
public interface ConfigRepository
        extends CrudRepository<ConfigTableData, String>, JpaSpecificationExecutor<ConfigTableData>,IConfigCustomRepository {

    @SuppressWarnings("unchecked")
    ConfigTableData save(ConfigTableData configData);

    Optional <ConfigTableData> findById(String string);

}

And that method called in RefreshTableJob.java

@Component
public class RefreshTableJob{

@Autowired
ConfigRepository configRepository;

public void execute(){

configRepository.refreshTable(queryText, lastRefreshTime, currentTime);

}
Radha S
  • 61
  • 1
  • 6
  • 2
    You aren't following the proper naming for this to work. If your interface is `IConfigCustomRepository` your implementation should be named `IConfigCustomRepositoryImpl` but you named it `ConfigCustomRepositoryImpl`. Also the `save` and `findById` can be removed (already present in `CrudRepository`, nor do the `@Repository` and `@Transactional` on the interface add anything. – M. Deinum May 12 '22 at 14:09

1 Answers1

1

(Updated after getting additional comments)

According to Spring documentation Custom Implementations for Spring Data Repositories

When you extend the default Spring CRUD repository with your custom interface:

interface UserRepositoryCustom {

  public void someCustomMethod(User user);
}


public interface UserRepository extends CrudRepository<User, Long>, UserRepositoryCustom {
    
      // Declare query methods here
    }

Spring, by default, tries to find its implementation in the class named <repositoryName> + Impl, i.e.

class UserRepositoryImpl implements UserRepositoryCustom {   
   public void someCustomMethod(User user) {
      // Your custom implementation
   }
}

See, Configuration: "If you use namespace configuration, the repository infrastructure tries to autodetect custom implementation fragments by scanning for classes below the package in which it found a repository. These classes need to follow the naming convention of appending the namespace element’s repository-impl-postfix attribute to the fragment interface name. This postfix defaults to Impl."

In your case, the implementation class should be named as IConfigCustomRepositoryImpl

Sve Kamenska
  • 371
  • 3
  • 6
  • Sorry.. Thats not the problem, while adding code snippet I missed here. Now I edited question – Radha S May 12 '22 at 13:33
  • I see.. could you please also check the error message? As it says about "ConfigCustomRepository.refreshTable" while you don't have it: you have IConfigCustomRepository (interface) and ConfigCustomRepositoryImpl (class) – Sve Kamenska May 12 '22 at 14:46
  • Sorry again.. I am editing question.. while removing fullyQualified name it got removed.. I have cross verified there is no issue with namingConvention of files and all.. – Radha S May 12 '22 at 14:51
  • Could you please show ConfigTableData ? Does it have, by chance, refreshTable field? – Sve Kamenska May 12 '22 at 15:11
  • ConfigTableData is entity class which is used for table's column mapping – Radha S May 12 '22 at 15:20
  • I am glad I could help :) Could you please accept the answer as a correct one then? @RadhaS – Sve Kamenska May 13 '22 at 11:22