title: Failed to convert property value of type java.lang.String to required type entity
body: <p>I'm new to Spring MVC and was trying to build a sample application.I was trying to save data to a table name EmployeeDetails which has one to one relationship with table Users. After submitting the form I was getting this error <strong>Field error in object 'employeeDetails' on field 'user': rejected value [Users]; codes [typeMismatch.employeeDetails.user,typeMismatch.user,typeMismatch.com.sachinmukherjee.spring_hibernate.entity.Users,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [employeeDetails.user,user]; arguments []; default message [user]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'com.sachinmukherjee.spring_hibernate.entity.Users' for property 'user'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.sachinmukherjee.spring_hibernate.entity.Users' for property 'user': no matching editors or conversion strategy found]]</strong></p>
  
  <p>User Table Structure</p>
  
  <pre><code>+--------------+--------------+------+-----+-------------------+----------------+
  | Field        | Type         | Null | Key | Default           | Extra          |
  +--------------+--------------+------+-----+-------------------+----------------+
  | id           | int(11)      | NO   | PRI | NULL              | auto_increment |
  | username     | varchar(20)  | NO   | UNI | NULL              |                |
  | password     | varchar(100) | NO   |     | NULL              |                |
  | created_at   | datetime     | YES  |     | CURRENT_TIMESTAMP |                |
  | userfullname | varchar(50)  | NO   |     | NULL              |                |
  +--------------+--------------+------+-----+-------------------+----------------+
  </code></pre>
  
  <p>Users Entity(Users.java)</p>
  
  <pre><code>@Entity
  @Table(name="users")
  public class Users {
  
      @Id
      @Column(name="id")
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private int id;
  
      @Column(name="username",unique = true)
      private String username;
  
      @Column(name="password")
      private String password;
  
      @Column(name="userfullname")
      private String userfullname;
  
      @Column(name = "created_at")
      private Timestamp created_at;
  
      @OneToOne(mappedBy = "user",
              cascade = CascadeType.PERSIST
              )
      private EmployeeDetails employee;
  
      public Users() {
      }
  
      public Users(String username, String password, String userfullname) {
          this.username = username;
          this.password = password;
          this.userfullname = userfullname;
      }
  
      public String getUsername() {
          return username;
      }
  
      public void setUsername(String username) {
          this.username = username;
      }
  
      public Timestamp getCreated_at() {
          return created_at;
      }
  
      public String getUserfullname() {
          return userfullname;
      }
  
      public void setUserfullname(String userfullname) {
          this.userfullname = userfullname;
      }
  
      public void setCreated_at(Timestamp created_at) {
          this.created_at = created_at;
      }
  
      public int getId() {
          return id;
      }
  
      public String getPassword() {
          return password;
      }
  
      public void setPassword(String password) {
          this.password = password;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public EmployeeDetails getEmployee() {
          return employee;
      }
  
      public void setEmployee(EmployeeDetails employee) {
          this.employee = employee;
      }
  
      @Override
      public String toString() {
          return "Users [id=" + id + ", username=" + username + ", userfullname=" + userfullname + ", created_at="
                  + created_at + "]";
      }
  }
  </code></pre>
  
  <p>EmployeeDetails Table Structure</p>
  
  <pre><code>+-------------+-------------+------+-----+-------------------+----------------+
  | Field       | Type        | Null | Key | Default           | Extra          |
  +-------------+-------------+------+-----+-------------------+----------------+
  | id          | int(11)     | NO   | PRI | NULL              | auto_increment |
  | first_name  | varchar(20) | NO   |     | NULL              |                |
  | middle_name | varchar(20) | YES  |     | NULL              |                |
  | last_name   | varchar(20) | YES  |     | NULL              |                |
  | mobile_no   | bigint(20)  | YES  |     | NULL              |                |
  | email       | varchar(50) | NO   |     | NULL              |                |
  | created_at  | datetime    | YES  |     | CURRENT_TIMESTAMP |                |
  | users_id    | int(11)     | NO   | MUL | NULL              |                |
  +-------------+-------------+------+-----+-------------------+----------------+
  </code></pre>
  
  <p>EmployeeDetails Entity Class(EmployeeDetails.java)</p>
  
  <pre><code>@Entity
  @Table(name="employee_details")
  public class EmployeeDetails {
  
      @Id
      @Column(name="id")
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private int id;
  
      @Column(name="first_name")
      private String first_name;
  
      @Column(name="middle_name")
      private String middle_name;
  
      @Column(name="last_name")
      private String last_name;
  
      @Column(name="mobile_no")
      private long mobile_no;
  
      @Column(name="email")
      private String email;
  
      @OneToOne(fetch = FetchType.EAGER,cascade = CascadeType.PERSIST)
      @JoinColumn(name = "users_id")
      private Users user;
  
      public EmployeeDetails() {
  
      }
  
      public EmployeeDetails(String first_name, String middle_name, String last_name, long mobile_no, String email) {
          this.first_name = first_name;
          this.middle_name = middle_name;
          this.last_name = last_name;
          this.mobile_no = mobile_no;
          this.email = email;
      }
  
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public String getFirst_name() {
          return first_name;
      }
  
      public void setFirst_name(String first_name) {
          this.first_name = first_name;
      }
  
      public String getMiddle_name() {
          return middle_name;
      }
  
      public void setMiddle_name(String middle_name) {
          this.middle_name = middle_name;
      }
  
      public String getLast_name() {
          return last_name;
      }
  
      public void setLast_name(String last_name) {
          this.last_name = last_name;
      }
  
      public long getMobile_no() {
          return mobile_no;
      }
  
      public void setMobile_no(long mobile_no) {
          this.mobile_no = mobile_no;
      }
  
      public String getEmail() {
          return email;
      }
  
      public void setEmail(String email) {
          this.email = email;
      }
  
      public Users getUser() {
          return user;
      }
  
      public void setUser(Users user) {
          this.user = user;
      }
  
      @Override
      public String toString() {
          return "EmployeeDetails [id=" + id + ", first_name=" + first_name + ", middle_name=" + middle_name
                  + ", last_name=" + last_name + ", mobile_no=" + mobile_no + ", email=" + email + ", user=" + user + "]";
      }
  }
  </code></pre>
  
  <p>My EmployeeDeatails Controller(EmployeeDetailsController.java)</p>
  
  <pre><code>@Controller
  @RequestMapping("/employee_details")
  public class EmployeeDetailsController {
  
      @Autowired
      EmployeeDetailsDAO employeeDetailsDAO;
      @Autowired
      UsersDAO usersDAO;
  
      @RequestMapping("/")
      public String home(Model model) {
          List&lt;EmployeeDetails&gt; employees = employeeDetailsDAO.getEmployees();
          model.addAttribute("employees", employees);
          return "employee_details/index";
      }
  
      @GetMapping("/add")
      public String add(Model model) {
          List&lt;Users&gt; users = usersDAO.getUsers();
          EmployeeDetails employee = new EmployeeDetails();
          model.addAttribute("userList",users);
          model.addAttribute("employee", employee);
          return "employee_details/add";
      }
  
      @PostMapping("/submit")
      public String submit(@ModelAttribute EmployeeDetails employee) {
          System.out.println(employee);
          employeeDetailsDAO.saveEmployee(employee);
          return "redirect:/employee_details/";
      }
  
  }
  </code></pre>
  
  <p>My EmployeeDetails DAO Class(EmployeeDetails DAO)</p>
  
  <pre><code>@Repository
  public class EmployeeDetailsDAOImp implements EmployeeDetailsDAO {
  
      @Autowired
      SessionFactory sessionFactory;
  
      @Transactional
      public List&lt;EmployeeDetails&gt; getEmployees() {
          Session session = sessionFactory.getCurrentSession();
          Query&lt;EmployeeDetails&gt; query = session.createQuery("from EmployeeDetails",EmployeeDetails.class);
          List&lt;EmployeeDetails&gt; employees = query.getResultList();
          System.out.println(employees);
          return employees;
      }
      @Transactional
      public void saveEmployee(EmployeeDetails employee) {
          Session session = sessionFactory.getCurrentSession();
          session.saveOrUpdate(employee);
  
      }
  }
  </code></pre>
  
  <p>My add.jsp page for filling the form</p>
  
  <pre><code>&lt;form action="${pageContext.request.contextPath}/employee_details/submit" method="POST" modelAttribute="employee" class="form-control"&gt;
          &lt;label&gt;Username&lt;/label&gt;
          &lt;select name="user" required="required"&gt;
               &lt;option&gt;Select User&lt;/option&gt;
              &lt;c:forEach var="user" items="${userList}"&gt;
                  &lt;option value=${user}&gt;${user.username}&lt;/option&gt;
              &lt;/c:forEach&gt;
          &lt;/select&gt;
          &lt;br&gt;&lt;br&gt;
          &lt;label&gt;First Name&lt;/label&gt;
          &lt;input type="text" name="first_name" /&gt;
          &lt;br&gt;&lt;br&gt;
          &lt;label&gt;Middle Name&lt;/label&gt;
          &lt;input type="text" name="middle_name" /&gt;
          &lt;br&gt;&lt;br&gt;
          &lt;label&gt;Last Name&lt;/label&gt;
          &lt;input type="text" name="last_name" /&gt;
          &lt;br&gt;&lt;br&gt;
          &lt;label&gt;Email&lt;/label&gt;
          &lt;input type="text" name="email" /&gt;
          &lt;br&gt;&lt;br&gt;
          &lt;button type="submit" class="btn btn-success"&gt;Save&lt;/button&gt;
      &lt;/form&gt;
  </code></pre>
  
  <p>My UsersDao Class</p>
  
  <pre><code>@Repository
  public class UsersDAOImp implements UsersDAO {
  
      //need to inject session factory
      @Autowired
      private SessionFactory sessionFactory;
  
      @Transactional
      public List&lt;Users&gt; getUsers() {
  
          //get the hibernate session
          Session session =  sessionFactory.getCurrentSession();
          //create a query 
          Query&lt;Users&gt; theQuery = 
                       session.createQuery("from Users", Users.class);
          //execute the query and get resultset
          List&lt;Users&gt; users = theQuery.getResultList();
          //return the result
          return users;
      }
  }
  </code></pre>
  
  <p>I'm not able to figure out why I'm getting this error and what should I pass in the method parameter of submit method.</p>