java - IllegalArgumentException 和 BeanCreationException : No property found for type error

标签 java spring spring-boot spring-mvc spring-data

我已在线搜索并尝试了一些建议的更正,但我的应用程序无法运行。它抛出 no property for type 异常以及 UnsatisfiedDependencyException:创建 bean 时出错。

当尝试运行代码时,它显示 Caused by: java.lang.IllegalArgumentException: Failed to create query for method public abstract java.math.BigDecimal com.Wallet.Repository.TransactionRepository.getBalance(java.lang.Long) !未找到 Transaction 类型的属性 getBalance

这是模型代码:

  @Entity
public class Transaction {

    @Id
    @GeneratedValue
    private Long id;

    private BigDecimal amount;

    private java.util.Date transactionDate;

    private Long  transactionReference;

    private String details;

    @ManyToOne
    private UserWallet wallet;

    public Transaction() {
        super();
    }

存储库

@Repository
public interface TransactionRepository extends CrudRepository <Transaction, Long>

{
    Optional<Transaction> getTransactionByRef(Long reference)throws UserNotFoundException;

    BigDecimal balanceByUserAccountID(Long accountId);

    List<Transaction> transactionsByUserAccountID(Long Id)throws UserNotFoundException;

    Transaction createTransaction(Transaction transaction) throws LowBalanceException;

    BigDecimal getBalance(Long id);

}

ServiceImpl

    @Service
public class TransactionServiceImpl  {

   @Autowired
   private TransactionRepository transRepo;

   public Object transactionByRef(Long reference) throws UserNotFoundException {
       return transRepo.getTransactionByRef(reference).orElseThrow(
               () -> new UserNotFoundException(String.format("transaction with ref '%d' doesnt exist", reference)));
   }
   @Transactional
 public Transaction createTransaction(Transaction transaction) throws LowBalanceException {

       BigDecimal balance = transRepo.getBalance(transaction.getWallet().getId());

       if (balance.add(transaction.getAmount()).compareTo(BigDecimal.ZERO) >= 0) {
           return transRepo.save(transaction);
       }

       throw new LowBalanceException(String.format("user's balance is %.2f and cannot perform a transaction of %.2f ",
               balance.doubleValue(), transaction.getAmount().doubleValue()));
       }

 public BigDecimal balanceByUserAccountID(Long accountId) {

       return transRepo.getBalance(accountId);
   }
 public Iterable<Transaction> getList() {
       return transRepo.findAll();
     }
 public Optional<Transaction> transactionsByUserAccountID(Long txnRef) throws UserNotFoundException  {
       return transRepo.getTransactionByRef(txnRef);
   }
   public Iterable<Transaction> transactions() {
       return transRepo.findAll();
   }

}

Controller

@RestController
@RequestMapping("v1/addMoney")
public class TransactionController {
@Autowired
private UserAccountServiceImp userRepo;

       @Autowired
       private TransactionServiceImpl transactRepo;

       @PostMapping("/{id}")
        public ResponseEntity addMoney(@PathVariable("id") Long userAccountId, @RequestBody TransactionDTO walletDTO) {

           Transaction saved;

           try {
                walletDTO.setUserAccountId(userAccountId);
                saved = transactRepo.createTransaction(TransactionMapper.dtoToDO(walletDTO));
            } catch (LowBalanceException ex) {
                Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, ex);
                return new ResponseEntity<String>(ex.getMessage(), HttpStatus.BAD_REQUEST);
            } catch (Exception ex) {
                Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, ex);
                return new ResponseEntity<String>(ex.getMessage(), HttpStatus.BAD_REQUEST);
            }
            return new ResponseEntity<TransactionDTO>(TransactionMapper.doToDTO(saved), HttpStatus.CREATED);

       }


       @GetMapping("v1/balance/{id}")
       public  BigDecimal getBalance(@PathVariable Long userAccountId){

             return transactRepo.balanceByUserAccountID(userAccountId);
          }
    }

最佳答案

Spring 数据存储库在底层发挥了很多作用。如果您在存储库接口(interface)中定义了名为 getBalance 的方法,则它期望您尝试从名为 Transaction< 的实体获取列 balance 的值。/.

只需在实体类中添加一个名为 balance 的字段即可:

@Entity
public class Transaction {

    @Id
    @GeneratedValue
    private Long id;

    private BigDecimal amount;

    private java.util.Date transactionDate;

    private Long  transactionReference;

    private String details;

    private BigDecimal balance;

    @ManyToOne
    private UserWallet wallet;

    public Transaction() {
        super();
    }

关于java - IllegalArgumentException 和 BeanCreationException : No property found for type error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58380556/

相关文章:

java - 对涉及 azure ad b2c 的安全 REST API 进行基于角色的访问

java - 无法将 2 个视频流合并到一个文件中

java - 用 Java 编程 Hangman,主线程错误异常(以及其他)

java - 与 mySQL 一起使用的自动 java 类

java - 访问 CXF Rest 服务中的 post 方法时出现问题

java - 无法在 JPA + Spring 中保留实体,没有错误

java - @RequestParam 因 HttpServletRequestWrapper 而失败

hibernate - Spring JPA 审计因多个数据源而失败

spring - DBUNIT 和 Spring.. 无法加载数据集文件

java - Spring Boot 找不到 IntelliJ