I am trying to implement the custom repository with JpaRepository in spring boot 3.2.3 by following the official documentation but is not working as expected.
My Repository Interface:
package com.test.accounts.repository;
import com.test.accounts.*;
import org.springframework.data.jpa.repository.JpaRepository;
public interface AccountsRepository extends JpaRepository<Accounts, Long>, AccountsRepositoryCustom {
}
My Customised Account repository:
package com.test.accounts.dao;
public interface AccountsRepositoryCustom {
void myCustomFindQuery();
}
Implementation of Customised repository:
package com.test.accounts.dao.impl;
import com.test.accounts.dao.AccountsRepositoryCustom;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AccountsRepositoryImpl implements AccountsRepositoryCustom {
private final Logger log = LoggerFactory.getLogger(AccountsRepositoryImpl.class);
@Override
public void myCustomFindQuery() {
log.debug("sharedAccountsMethod");
}
}
Account Service code:
package com.test.accounts.service.impl;
import com.test.accounts.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.Random;
@Service
public class AccountServiceImpl implements IAccountService {
private final Logger log = LoggerFactory.getLogger(AccountServiceImpl.class);
private final AccountsRepository accountsRepository;
private final CustomerRepository customerRepository;
private Accounts createNewAccount(Customer customer) {
Accounts newAccount = new Accounts();
newAccount.setCustomerId(customer.getCustomerId());
long randomAccNumber = 1000000000L + new Random().nextInt(900000000);
newAccount.setAccountNumber(randomAccNumber);
newAccount.setAccountType(AccountConstants.SAVINGS);
newAccount.setBranchAddress(AccountConstants.ADDRESS);
newAccount.setCreatedAt(LocalDateTime.now());
newAccount.setCreatedBy("Test");
return newAccount;
}
public AccountServiceImpl(AccountsRepository accountsRepository, CustomerRepository customerRepository) {
this.accountsRepository = accountsRepository;
this.customerRepository = customerRepository;
}
/**
* @param customerDto {@link CustomerDto}
*/
@Override
public void createAccount(CustomerDto customerDto) {
log.info("LOGSSSSSSSS");
Customer customer = CustomerMapper.mapToCustomer(customerDto, new Customer());
this.customerRepository.findByMobileNumber(customerDto.getMobileNumber()).ifPresent(cust -> {
throw new CustomerAlreadyExistsException(
"Customer already registered with given mobileNumber:" + cust.getMobileNumber());
});
customer.setCreatedAt(LocalDateTime.now());
customer.setCreatedBy("Test");
Customer savedCustomer = this.customerRepository.save(customer);
this.accountsRepository.save(createNewAccount(savedCustomer));
}
}
Controller class:
package com.test.accounts.controller;
import com.test.accounts.*;
import org.springframework.*;
@RestController
@RequestMapping(path = "/v1/api", produces = { MediaType.APPLICATION_JSON_VALUE })
public class AccountsController {
private final IAccountService accountService;
public AccountsController(IAccountService accountService) {
this.accountService = accountService;
}
@PostMapping(value = "customer", consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<GenericAllResponseDto> createCustomer(@RequestBody CustomerDto customerDto) {
this.accountService.createAccount(customerDto);
GenericAllResponseDto responseDto = new GenericAllResponseDto();
responseDto.setStatusCode(String.valueOf(HttpStatus.CREATED.value()));
responseDto.setStatusMsg(HttpStatus.CREATED.toString());
return ResponseEntity.status(HttpStatus.CREATED).body(responseDto);
}
}
but I run my project it gives me the error:
2024-03-17T14:43:29.234+05:30 ERROR 75477 --- \[accounts\] \[ restartedMain\] o.s.boot.SpringApplication : Application run failed
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accountsRepository' defined in com.course.accounts.repository.AccountsRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Could not create query for public abstract void com.course.accounts.dao.AccountsRepositoryCustom.myCustomFindQuery(); Reason: Failed to create query for method public abstract void com.course.accounts.dao.AccountsRepositoryCustom.myCustomFindQuery(); No property 'myCustomFindQuery' found for type 'Accounts'
at
Caused by: org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract void com.course.accounts.dao.AccountsRepositoryCustom.myCustomFindQuery(); Reason: Failed to create query for method public abstract void com.course.accounts.dao.AccountsRepositoryCustom.myCustomFindQuery(); No property 'myCustomFindQuery' found for type 'Accounts'
Caused by: java.lang.IllegalArgumentException: Failed to create query for method public abstract void com.course.accounts.dao.AccountsRepositoryCustom.myCustomFindQuery(); No property 'myCustomFindQuery' found for type 'Accounts'
- As custom repository is not the part of JPA repo why its trying to create query from method name?
I have tried the same way it was mentioned in this spring boot official DOC
- is it a bug in spring-data-jpa or spring boot?