Radha S
Radha S

Reputation: 81

EntityManager in CustomRepository with CRUDRepository throwing error

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);

}

Upvotes: 1

Views: 529

Answers (1)

Idol
Idol

Reputation: 570

(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

Upvotes: 1

Related Questions