Cześć! Potrzebuję zrobić tak, aby za pomocą jednego formularza, uzupełnić 3 tabele(Account, Customer, Users) które są od siebie wzajemnie zależne.
W tym celu, utworzyłem klasę RegisterObject, która agreguje pod sobą wszystkie 3 encje:
package main.com.java.entity;
public class RegisterObject {
private Account account;
private Customer customer;
private Users users;
public RegisterObject(Account account, Customer customer, Users users) {
this.account = account;
this.customer = customer;
this.users = users;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public Users getUsers() {
return users;
}
public void setUsers(Users users) {
this.users = users;
}
}
Jednak po uzupełnieniu formularza w widoku, wartości te nie są przeniesione na model, przez co zwraca mi nullowe wartości na parametrach obiektów Account, Users, Customer.
Metody w kontrolerze:
@GetMapping("/showFormForAddAccount")
public String showFormForAddAccount(Model theModel){
RegisterObject registerObject = new RegisterObject(new Account(), new Customer(), new Users());
theModel.addAttribute("registerObject", registerObject);
return "account-form";
}
@PostMapping("/saveAccount")
public String saveAccount(@ModelAttribute("registerObject") RegisterObject registerObject) {
registerObject.getAccount().setAccountNumber(accountNumberGenerator.generateAccountNumber());
registerObject.getUsers().setPassword(passwordGenerator.generatePassword());
registerObject.getCustomer().setIdOfAccount(registerObject.getAccount().getAccountID());
registerObject.getUsers().setUsername(registerObject.getAccount().getUsername());
accountService.addAccount(registerObject.getAccount());
usersService.addUser(registerObject.getUsers());
customerService.addCustomer(registerObject.getCustomer());
return "redirect:/customer/list";
}
Formularz:
<div class="table-container">
<form:form action="saveAccount" modelAttribute="registerObject" method="POST">
<!-- need to associate this data with customer id -->
<form:hidden path="account.accountID" />
<table>
<tbody>
<tr>
<td><label>Balance:</label></td>
<td><form:input path="account.balance" /></td>
</tr>
<tr>
<td><label>Username:</label></td>
<td><form:input path="account.username" /></td>
</tr>
<tr>
<td><label>First name:</label></td>
<td><form:input path="customer.firstName" /></td>
</tr>
<tr>
<td><label>Last name:</label></td>
<td><form:input path="customer.lastName" /></td>
</tr>
<tr>
<td><label>Country:</label></td>
<td><form:input path="customer.country" /></td>
</tr>
<tr>
<td><label>User Enable:</label></td>
<td><form:input path="users.enabled" /></td>
</tr>
<tr>
<td><label></label></td>
<td><input type="submit" value="Save" class="save" /></td>
</tr>
</tbody>
</table>
Czy ktoś zna rozwiązanie tego problemu?
Dodam, że jeżeli ktoś zna lepszy sposób na uzupełnienie w jednym czasie 3 tabeli, to chętnie go poznam. :)