Ad

Failed To Convert Property Value Of Type 'java.lang.String'

- 1 answer

What am I doing wrong here?

<select th:field="*{role}">
    <option value="#" disabled="disabled" selected="selected">Role...</option>
    <option th:each="r : ${roles}" th:value="${r}" th:text="${r.name}">Developer</option>
</select>

I am getting this error:

Field error in object 'collaborator' on field 'role': rejected value
[33]; codes
[typeMismatch.collaborator.role,typeMismatch.role,typeMismatch.com.imprender.instateam.model.Role,typeMismatch];
arguments
[org.springframework.context.support.DefaultMessageSourceResolvable:
codes [collaborator.role,role]; arguments []; default message [role]];
default message [*Failed to convert property value of type
'java.lang.String' to required type
'com.imprender.instateam.model.Role' for property 'role'*; nested
exception is java.lang.IllegalStateException: Cannot convert value of
type 'java.lang.String' to required type
'com.imprender.instateam.model.Role' for property 'role': no matching
editors or conversion strategy found]

It says:

**Failed to convert property value of type 'java.lang.String' to required type 'com.imprender.instateam.model.Role' for property
'role'**

but I don't understand where I am doing that.

I think I am not properly transmiting the value selected in the . I thought the object sent would be the one declared in the valueoption that gets selected, but obviously I got it wrong and can't find the way to do it properly.

The model:

package com.imprender.instateam.model;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

@Entity
public class Collaborator {

    @Id
    //Todo: check strategy
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotNull
    @Pattern(regexp = "([A-Z][a-zA-Z]*\\s*)+")
    private String name;

    //Todo: check, do we want to create a new table to associate values?

    @NotNull
    @ManyToOne
    private Role role;

    public Collaborator() {
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }
}
Ad

Answer

Your problem is that in your select says the field is a role, entity type Role, but in your options the value is an ID, some primitive value, then doesnt match. You can change to this

<select th:field="*{role.id}">
    <option value="#" disabled = "disabled" selected="selected">Role...</option>
    <option th:each="r : ${roles}" th:value="${r.id}" th:text="${r.name}">Developer</option>
</select>
Ad
source: stackoverflow.com
Ad