Ere*_*ent 2 java jpa spring-boot
我是java和java开发的新手,我刚刚开始自学springboot,我想将数据保存到具有一对多关系的表中。我不知道如何去做,因为这与一对一的关系不同。
这是我的代码:
账户类别
Entity
@Table(name = "Account")
public class Account implements Serializable {
@JsonIgnore
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int accountID;
@JsonIgnore
private String accountName;
@JsonIgnore
private String accountType;
private Boolean openAccount;
@JsonIgnore
private Double balance;
@JsonIgnore
@OneToOne(mappedBy = "account")
private Customer customer;
@OneToMany(mappedBy = "account")
private List<AccountTransactions> Transactions ;
public Account() {
}
public Account(String accountType, String accountName, Boolean openAccount, Double balance, Customer customer) {
this.accountType = accountType;
this.accountName = accountName;
this.openAccount = openAccount;
this.balance = balance;
this.customer = customer;
}
... ommited getters and setters for brevity
Run Code Online (Sandbox Code Playgroud)
账户交易类
@Entity
@Table(name = "Transactions")
public class AccountTransactions implements Serializable {
@JsonIgnore
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int transactionID;
private int transactionCode;
private String transactionDate;
private String transactionType;
private String transactionTime;
private String transactionAmount;
private String transactionTo;
private String transactionFrom;
@ManyToOne
@JoinColumn(name="accountID")
private Account account;
public AccountTransactions() {
}
public AccountTransactions(int transactionCode, String transactionDate, String transactionType, String transactionTime, String transactionAmount, String transactionTo, String transactionFrom) {
this.transactionCode = transactionCode;
this.transactionDate = transactionDate;
this.transactionType = transactionType;
this.transactionTime = transactionTime;
this.transactionAmount = transactionAmount;
this.transactionTo = transactionTo;
this.transactionFrom = transactionFrom;
}
... ommited getters and setters for brevity
Run Code Online (Sandbox Code Playgroud)
账户服务类
public TransactionResponse sendPoints(Transfer transferObject){
Account account = accountRepository.findByAccountName(transferObject.getFromName());
Account sendingToAccount = accountRepository.findByAccountType(transferObject.getToType());
AccountTransactions transacts = new AccountTransactions();
DateUtility todayDate = new DateUtility();
if (account == null || sendingToAccount == null) {
throw new ResourceNotFoundException(transferObject.getToType, "account not found");
}else{
Boolean transferSucceeded = Transactions.transfer(from, transferObject.getToType,transferObject.getAmount().toString());
if (transferSucceeded){
newAccountBalance = Double.parseDouble(Transactions.getBalance(from));
transacts.setTransactionAmount(transferObject.getAmount().toString());
transacts.setTransactionCode(01014);
transacts.setTransactionDate(todayDate.getCurrentDateTime());
transacts.setTransactionFrom("");
transacts.setTransactionTime(todayDate.getCurrentDateTime());
transacts.setTransactionTo(sendingToAccount.getCustomer().getName());
transacts.setTransactionType("Transfer");
// Here is where i need to be able to save the
// transact object but i am not sure how to go about it.
accountRepository.save(account);
status = true;
return new TransactionResponse(status, transferObject.getAmount().toString()+" Successfuly Sent ",newAccountBalance);
}else{
return new TransactionResponse(status, "Insufficient Funds, Attempt to transfer "+transferObject.getAmount().toString()+" while current balance = "+userBalance.toString(), userBalance);
}
}
}
Run Code Online (Sandbox Code Playgroud)
更改注释以支持级联
@OneToMany(mappedBy = "account", cascade = CascadeType.ALL)
private List<AccountTransactions> Transactions ;
Run Code Online (Sandbox Code Playgroud)
然后将交易对象添加到您的帐户对象中,然后保存帐户对象。这将保存帐户对象以及交易。
小智 7
您必须设置账户与交易的关系并保存:
transacts.setAccount(account);
transactRepo.save(transacts);
Run Code Online (Sandbox Code Playgroud)
您不必调用,accountRepository.save(account);因为您没有更改该对象中的任何内容(至少在您粘贴的代码中)。我不知道你从哪里得到变量from,它没有在任何地方定义。
顺便说一句,我不知道为什么您调用类 AccountTransactions,当该类型的对象仅包含有关一笔交易的信息时。将其重命名为 AccountTransaction。
| 归档时间: |
|
| 查看次数: |
15617 次 |
| 最近记录: |