我已在线搜索并尝试了一些建议的更正,但我的应用程序无法运行。它抛出 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/