Skip to content
Advertisement

Get values from User and update into mysql database in Springboot Controller

I’m developing a Banking system Project Where User Should be able to create an account in any available branch. Branch Creation will be maintained by admin. I had two tables(Entities) like BranchDetails and AccountDetails. db_Structure The BranchId Column is the primary key in BranchDetails and Foreign key in AccountDetails. Now When user Creates an account, he will input the Preferred Branch name and the AccountHolder name. I had to Insert this Account in AccountDetails Table which matching the branchId which the user had entered.

How do i achive this. So far i have tried,

BranchDetails.java

@Entity
@Table( name = "branchdetails")
public class BranchDetails {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int branchId;
    @Column( name = "branchName")
    private String branchName;
    @OneToOne(mappedBy = "branchdetails")
    private AccountDetails accountDetails;

/getters and setters
}

AccountDetails.java

@Entity
@Table(name = "accountdetails")
public class AccountDetails {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int customerId;
    @Column(name = "customerName")
    private String customerName;

    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "branchId")
    private BranchDetails branchdetails;

/getters and setters
}

Controller

@Controller
public class ApiController{

@RequestMapping(value = "/branchcreation", method = RequestMethod.POST
          ,consumes = {"application/x-www-form-urlencoded"})
  @ResponseBody
  public String brCreation( BranchDetails br, String branchname){
      br.setBranchName(branchname);
      branchrepository.save(br);
      return "Sucesspage";
  }

  @RequestMapping(value = "/accountcreation", method = RequestMethod.POST)
  @ResponseBody
  public String AcCreation(AccountDetails ad,BranchDetails br, String branchname,String customername){
    br.setBranchName(branchname);
    ad.setCustomerName(customername);
    accountrepository.save(ad);
    return "Sucesspage";
  }

}

Advertisement

Answer

Prepare AcCreation method to receive branch id (the branches already exist, so you could send their ids and names to your frontend form’s select component) and customer name (provided by the user in the input field):

It would look like this (I changed the name to createAccount, because it sounds naturally, there is no need to use shortcuts in method’s name, but in the end it’s your choice):

  @RequestMapping(value = "/account-creation", method = RequestMethod.POST)
  @ResponseBody
  public String createAccount(String customerName, Integer branchId){
    accountRepository.createAccount(customerName, branchId);
    return "Sucesspage";
  }

Look at the removed code from the service method.

Details connected with creation on the database should be contained by database access layer, in this case the class AccountRepository and database layer methods should be called by service’s methods – in your case we left out the service class).

So you would create Account instance inside its method and then set the branchId field.

Or you could do something like this (you would have to have two separate repositories, one for AccountDetails, second for BranchDetails entities):

  @RequestMapping(value = "/account-creation", method = RequestMethod.POST)
  @ResponseBody
  public String createAccount(String customerName, Integer branchId){
    BranchDetails branchDetails = branchRepository.find(branchId);
    AccountDetails accountDetails = new AccountDetails();
    accountDetails.setCustomerName(customerName);
    accountDetails.setBranchDetails(branchDetails);
    accountRepository.save(accountDetails);
    return "Sucesspage";
  }
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement