我是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
AccountTransactions类
@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
帐户服务类
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);
}
}
}
答案 0 :(得分:2)
您必须设置帐户关系以进行交易并保存:
transacts.setAccount(account);
transactRepo.save(transacts);
您不必致电accountRepository.save(account);
,因为您没有更改该对象中的任何内容(至少在您粘贴的代码中)。而且我不知道变量from
的位置,它在任何地方都没有定义。
顺便说一下,我不知道你为什么要调用类AccountTransactions,因为该类型的对象只包含一个事务的信息。将其重命名为AccountTransaction。
答案 1 :(得分:2)
更改注释以支持级联
@OneToMany(mappedBy = "account", cascade = CascadeType.ALL)
private List<AccountTransactions> Transactions ;
然后将事务对象添加到您的帐户对象,然后保存帐户对象。这将保存帐户对象以及交易。