Reputation: 25
i want to update object from my database without inserting new to the DB.
here are 2 methods:
@RequestMapping("/edit/{id}")
public ModelAndView showEditProductPage(@PathVariable(name = "id") Integer id, Model model) {
ModelAndView modelAndView = new ModelAndView("edit_customer");
Customer customer = customerService.getCustomer(id);
//Customer customer = customerRepository.getOne(id);
//customer.setCustomerId(customerRepository.getOne(id).getCustomerId());
System.out.println(customer.getCustomerId());
System.out.println(customer.toString());
model.addAttribute("customer", customer);
modelAndView.addObject("customer", customer);
return modelAndView;
}
output is: id=1 Customer{id=1, firstName='Krzysztof', address='ulica 1', phoneNumber='123456789'}
@RequestMapping("/update/{id}")
public String updateCustomer(@PathVariable(name = "id") Integer id,
@ModelAttribute("customer") Customer customer){
//System.out.println(id);
//customer.setCustomerId(id);
System.out.println("____________________");
System.out.println("customer id " + customer.getCustomerId());
System.out.println("____________________");
customer.setCustomerId(id);
System.out.println("customer id " + customer.getCustomerId());
System.out.println("owner " + customer.getOwner());
System.out.println("address " + customer.getAddress());
System.out.println("phone number " + customer.getAddress());
System.out.println("to string: " + customer.toString());
System.out.println("id " + id);
customerService.save(customer);
return "redirect:/";
}
output after second method is: id=1 Customer{id=null, firstName='Krzysztof', address='ulica 1zzz', phoneNumber='123456789'}
customer repository extends JPA repository, save method:
public void save(Customer customer) {
customerRepository.save(customer);
}
thymeleaf template:
!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8" />
<title>Edit Customer</title>
<link rel="stylesheet" type="text/css" th:href="@{/table.css}" />
</head>
<body>
<div align="center">
<h1>Edit Customer</h1>
<a href="/">Go Back</a>
<br />
<form action="#" th:action="@{'/update/' + ${id}}" th:object="${customer}"
method="post">
<table border="0" cellpadding="10">
<tr>
<td>Customer ID:</td>
<td>
<input type="text" th:field="*{customerId}" readonly="readonly" />
</td>
</tr>
<tr>
<td>First Name:</td>
<td>
<input type="text" th:field="*{owner}" />
</td>
</tr>
<tr>
<td>Address:</td>
<td><input type="text" th:field="*{address}" /></td>
</tr>
<tr>
<td>Phone Number:</td>
<td><input type="text" th:field="*{phoneNumber}" /></td>
</tr>
<tr>
<td colspan="2"><button type="submit">Save</button> </td>
</tr>
</table>
</form>
</div>
</body>
</html>
customer entity class:
package com.praca.manager.entity;
import javax.persistence.*;
import java.util.List;
@Entity
@Table(name = "customer")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer customerId;
@Column(name = "owner")
private String owner;
@Column(name = "address")
private String address;
@Column(name = "phone_number")
private String phoneNumber;
@OneToMany(mappedBy = "customer")
List<Details> details;
public Customer(){
}
public Customer(Integer customerId,
String owner,
String address,
String phoneNumber){
this.customerId = customerId;
this.owner = owner;
this.address = address;
this.phoneNumber = phoneNumber;
}
public Integer getCustomerId() {
return customerId;
}
public void setCustomerId(Integer id){
this.customerId = customerId;
}
public String getOwner() {
return owner;
}
public void setOwner(String firstName) {
this.owner = firstName;
}
public String getAddress() {
return address;
}
public void setAddress(String address){
this.address = address;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
@Override
public String toString() {
return "Customer{" +
"id=" + customerId +
", firstName='" + owner + '\'' +
", address='" + address + '\'' +
", phoneNumber='" + phoneNumber + '\'' +
'}';
}
}
here ID field is also '1' sql script:
create table customer(
customer_id int auto_increment primary key not null,
owner varchar(200) not null,
address varchar(200) not null,
phone_number int not null
);
despite i try to set id to 1 or any other number, it is still null. Can anyone tell me why and how to fix it?
Upvotes: 0
Views: 161
Reputation: 556
The issue is in the setter of your Customer class:
public void setCustomerId(Integer id){
this.customerId = customerId;
}
You assigned the field to itself, that's why it's always null. Should be "this.customerId = id" :).
Upvotes: 2