Search code examples
javaspring-bootjpa

Springboot How to save data in a OneToMany Relationship


I am a new to java and java development, i just started teaching myself springboot and i would like to save data to a table that has a one to many relationship. I am not sure how to go about doing that as this is different from a one to one relationship.

This is my code :

Account Class

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 Class

@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 class

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

           }
    }


}

Solution

  • You have to set account relation to transacts and save it:

    transacts.setAccount(account);
    transactRepo.save(transacts);
    

    You don't have to call accountRepository.save(account); because you are not changing anything in that object (at least in code you pasted). And I don't know from where you get variable from, it's not defined anywhere.

    Btw I don't know why you called class AccountTransactions, when object of that type contains info about just one transaction. Rename it to AccountTransaction.