Jak rozwiązać taki problem:
Mam taką klasę:
public class Customer{
private int id;
@NotNull
@Size(min=2, max=15)
private String name;
@Min(value = 18)
private int age;
@Min(value = 1)
private double salary;
MultipartFile image;
taki kontroler do dodawania "Customeró" - z racji problemu nie jest on ukońćzony
@RequestMapping(value = "/customers/add", method = RequestMethod.GET)
public String getAddNewCustomerForm(@ModelAttribute("customer") Customer customer) {
return "add";
}
@RequestMapping(value = "/customers/add", method = RequestMethod.POST)
public String processAddCustomerForm(@Valid @ModelAttribute("customer") Customer customer, BindingResult result, HttpServletRequest request) {
MultipartFile customerImage = customer.getImage();
String rootDirectory = request.getSession().getServletContext().getRealPath("/");
if(result.hasErrors()){
return "add";
}
else {
repository.addCustomer(customer);
if (customerImage!=null && !customerImage.isEmpty()) {
try {
customerImage.transferTo(new File(rootDirectory+"resources\\images\\"+customer.hashCode() + ".jpg"));
} catch (Exception e) {
throw new RuntimeException("Product Image saving failed", e);
}
}
return "redirect:/customers";
}
}
taki formularz za pomocą którego daję obiekty do bazy danych i wyświetlam je potem w widoku /customers(skracam kod)
<div class="form-group">
<label class="control-label col-lg-2" for="name">Name</label>
<div class="col-lg-10">
<form:input id="name" path="name" type="text" class="form:input-large" />
<form:errors path="name" cssClass="text-danger"/>
</div>
</div>
<div class="form-group">
<label class="control-label col-lg-2" for="age">Age</label>
<div class="col-lg-10">
<div class="form:input-prepend">
<form:input id="age" path="age" type="text" class="form:input-large"/>
<form:errors path="age" cssClass="text-danger"/>
</div>
</div>
</div>
<div class="form-group">
<label class="control-label col-lg-2" for="salary">Salary</label>
<div class="col-lg-10">
<form:input id="salary" path="salary" type="text" class="form:input-large"/>
<form:errors path="salary" cssClass="text-danger"/>
</div>
</div>
<div class="form-group">
<label class="control-label col-lg-2" for="salary">Choose file</label>
<div class="col-lg-10">
<form:input id="image" path="image" type="file" class="form:input-large" />
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<input type="submit" id="btnAdd" class="btn btn-primary" value ="Add"/>
</div>
</div>
Jak widać NIE OKREŚLAM W NIM ID, bo jest generowane automatycznie w bazie danych
CREATE TABLE CUSTOMERS(
ID INT NOT NULL auto_increment,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);
metoda sql dodawania Customera do bazy danych wygląda tak:
@Override
public void addCustomer(Customer customer) {
//language=SQL
String SQL_INSERT_CUSTOMER="INSERT INTO CUSTOMERS(NAME,AGE,SALARY) VALUES (?,?,?)";
String name = customer.getName();
int age = customer.getAge();
double salary = customer.getSalary();
jdbcTemplate.update(SQL_INSERT_CUSTOMER,name,age,salary);
}
Problem jest taki, nie chciałem obciążać się generowaniem klcza głównego i zepchnąłem to na bazę danych. Jednak teraz gdy w kontrolerze wyciągam dane z forumularza i nie mam tam ID to jest problem. Bo potrzebuję unikalnego kodu aby przypisać do Customera obrazek.
Głupio mi dopisać w kontrolerze metodę, która będzie pytać o ID Customera, który dopiero został stworzony. Nie wiem jak tutaj automatycznie generować klucz główny aby od razu mieć do niego dostęp. Jakieś pomysły?