本文介绍了异常:java.lang.IllegalStateException:无法转换 OneToOne 映射的类型值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 springboot web 应用程序,我想为 OnetoOne 关系表插入值,我有员工和地址实体,我可以像下面这样在数据库中插入值,我无法使用 web 表单插入值,

I am creating a springboot web app, I want to insert values for OnetoOne relation table,I have Employee and Address Entities, I am able to insert values in DB like bellow, I am unable to insertvalues using web form,

    Employee employee= new Employee();
    employee.setEmployeeName("Barry Bingel");
    employee.setEmail("[email protected]");
    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);

但我想通过网络表单插入相同的数据,如下所述

But I want to insert the same data through web form, mentioned bellow

<%@ 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>

但是我收到了下面提到的错误.

But I am getting error mentioned bellow.

 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]]

下面提到的我的员工和地址实体类

My Employee and Address Entity Class mentioned bellow

地址.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 类我的控制器.JAVA

MyController ClassMyController.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("[email protected]");
//      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;

    }

}

推荐答案

看起来你的表单只有一个用于 Address 的文本字段,它是一个具有多个属性的复合对象

It looks like your form has single text field for the Address, which is a composite object with multiple properties

<td><form:input path="address" placeholder="address" /></td>

Spring 无法将 String 转换为预期的对象

Spring cannot convert a String to the expected object

Cannot convert value of type 'java.lang.String' to required type 'com.tcdc.Entity.Address' for property 'address': no matching

您应该为地址提供多个字段(address.street、address.city)

You should provide multiple fields for the address (address.street, address.city)

这篇关于异常:java.lang.IllegalStateException:无法转换 OneToOne 映射的类型值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 00:14