Я создаю веб-приложение springboot, хочу вставить значения для таблицы отношений OnetoOne, у меня есть объекты Employee и Address, я могу вставить значения в БД, как показано ниже, я не могу вставить значения с помощью веб-формы,
Employee employee= new Employee();
employee.setEmployeeName("Barry Bingel");
employee.setEmail("barry.cs2017@gmail.com");
employee.setSalary(50000.00);
Address address1 = new Address();
address1.setCity("Chennai");
address1.setPincode(9087727L);
address1.setState("Tamilnadu");
address1.setStreet("Park Street");
employee.setAddress(address1);
empServices.save(employee);
Но я хочу вставить те же данные через веб-форму, упомянутую ниже
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<html>
<head>
<title>New Student Form</title>
</head>
<body>
<form:form action="save" method="post" modelAttribute="employee">
<table >
<tr>
<td>employeeName</td>
<td><form:input path="employeeName" placeholder="Name" /></td>
</tr>
<tr>
<td>Email</td>
<td><form:input path="email" placeholder="Email"/></td>
</tr>
<tr>
<td>Salary</td>
<td><form:input path="salary" placeholder="salary"/></td>
</tr>
<tr>
<td>Address</td>
<td><form:input path="address" placeholder="address" /></td>
</tr>
</table>
<input type="submit" value="Save">
</form:form>
</body>
</html>
Но я получаю ошибку, упомянутую ниже.
Field error in object 'employee' on field 'address': rejected value [Address [street=Park Street, city=Chennai, state=Tamilnadu, pincode=9087727]]; codes
[typeMismatch.employee.address,typeMismatch.address,typeMismatch.com.tcdc.Entity.Address,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes
[employee.address,address]; arguments []; default message [address]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'com.tcdc.Entity.Address' for property
'address'; nested exception is java.lang.IllegalStateException: Cannot convert value of type
'java.lang.String' to required type 'com.tcdc.Entity.Address' for property 'address': no matching
editors or conversion strategy found]]
Мой сотрудник и Класс сущности адреса, упомянутый ниже
Адрес. JAVA
@Entity
@Table(name="address_table")
public class Address {
@Id
@Column(name="adress_id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer addressId;
@Column(name = "street_name",length=50)
private String street;
@Column(name = "city_name",length=50)
private String city;
@Column(name = "state_name")
private String state;
@Column(name = "pin_code")
private Long pincode;
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public Long getPincode() {
return pincode;
}
public void setPincode(Long pincode) {
this.pincode = pincode;
}
@Override
public String toString() {
return "Address [street=" + street + ", city=" + city + ", state=" + state + ", pincode=" + pincode + "]";
}
}
Сотрудник. JAVA
@Entity
@Table(name="employee_table")
@DynamicUpdate
public class Employee {
@Id
@Column(name="employee_id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer employeeId;
@Column(name="employee_name",length=200,nullable=false)
private String employeeName;
@Column(name="email")
private String email;
@Column(name="salary")
private Double salary;
@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name="address_id")
private Address address;
public Integer getEmployeeId() {
return employeeId;
}
public void setEmployeeId(Integer employeeId) {
this.employeeId = employeeId;
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
public void setAddress(Address address) {
this.address = address;
}
public Address getAddress() {
return address;
}
@Override
public String toString() {
return "Employee [employeeId=" + employeeId + ", employeeName=" + employeeName + ", email=" + email
+ ", salary=" + salary + ", address=" + address + "]";
}
}
MyController Class MyController. JAVA
@Controller
public class MyController {
@Autowired
EmpServices empServices;
@GetMapping("/home3")
public String methodOne(Model model) {
Employee employee= new Employee();
// employee.setEmployeeName("Barry Bingel");
// employee.setEmail("barry.cs2017@gmail.com");
// employee.setSalary(50000.00);
Address address1 = new Address();
// address1.setCity("Chennai");
// address1.setPincode(9087727L);
// address1.setState("Tamilnadu");
// address1.setStreet("Park Street");
// employee.setAddress(address1);
// empServices.save(employee);
model.addAttribute("employee", employee);
return "form";
}
@PostMapping("/save")
public String saveMethod(@ModelAttribute("employee") Employee themployee) {
//themployee.setAddress(themployee.getAddress());
System.out.println(themployee.getAddress());
return null;
}
}