java - Springboot如何以OneToMany关系保存数据

标签 java spring-boot jpa

我是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

AccountService 类

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);

           }
    }


}

最佳答案

更改注释以支持级联

@OneToMany(mappedBy = "account", cascade = CascadeType.ALL)
private List<AccountTransactions> Transactions ;

然后将交易对象添加到您的帐户对象中,然后保存帐户对象。这将保存帐户对象以及交易。

关于java - Springboot如何以OneToMany关系保存数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49698899/

相关文章:

java - GreenDao 的可移植性和连接性

java - 通过 JNA 检索数组

java - 图像和字符串数组的 ListView 不起作用

java - 基于列值的 JPA 目标实体类型

java - 使用复合主键更新 JPA 实体会导致重复输入错误

java如何使用正则表达式替换N个空格

java - 仅来 self 的包的 Spring Boot 堆栈跟踪日志记录过滤器

mysql - Spring Data JPA 基于可编辑位置属性自动排序行

postgresql - 在 docker : org. postgresql.util.PSQLException 中运行时出现异常:致命:用户 "hamzabelmellouki"的密码身份验证失败

java - JPA 不抛出 EntityExistsException 但生成重复行(自动生成 PK)