Ad

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

I have two entity that refers to one another using a primary key which is an integer from one entity. I am not sure if I am doing it the wright way or not.

Below is the entity that referred to with Primary key id as int

@Entity
@Table(name="michango_kanda")
public class Kmichango  {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int id;


@OneToMany(cascade = CascadeType.ALL, mappedBy = "kandaMchango" )
private List<Jmichango> jumuiyaMichango = new ArrayList<>();

//Constructor,toString,getter and setter,.......}        

Below is the entity we set the foreign key as Kmichango kandaMchango from the above entity.

@Entity
@Table(name = "michango_jumuiya")
public class Jmichango  {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int id;

@ManyToOne
@JoinColumn(name = "kandamchangoID")
private Kmichango kandaMchango;

//Constructor,toString,getter and setter,.......}   

here is part of the form where I submit the data provided by the user in jumuiya_michango_form.html

            <form  class="form-horizontal" method="POST" action="/save-michango-jumuiya"
             style="max-width: 600px; margin: 0 auto;" >
            <div class="control-group">
            <label class="control-label" for="kandaMchango">Code Ya Mchango</label>
            <div class="select">
             <select id="kandaMchango" name="kandaMchango" required="true">
                 <option value="none" selected disabled hidden > 
                     Chagua Code ya Mchango
                 </option> 
                 <th:block th:each="kanda : ${kandaMichango}">
                  <option th:each="text : ${kanda.id}" th:value="${text}" th:text="${text}" ></option>
                  </th:block>
                 </select>
             </div>
         </div>
           <input type="submit" value="Submit">
        </form>

below are the two methods from my controller used to link to the form and to post the data

@GetMapping("/michango/jumuiya/add")
public String addJumuiyaMichango(Model model){
 List<Kmichango> kandaMichango = kMichangoRepository.findAll();
 List<Jumuiya> jumuiya =  jumuiyaRepo.findAll();
  model.addAttribute("kandaMichango", kandaMichango);
  model.addAttribute("jumuiya", jumuiya);
  return "jumuiya_michango_form";
}

@PostMapping("/save-michango-jumuiya")
public String saveJumuiyaMichango(Jmichango jumuiyaMichango, BindingResult result){

  if(result.hasErrors()){
    System.out.println(result);
    return "jumuiya_michango_form";
  }
  jMichangoRepository.save(jumuiyaMichango);
  return "index";
} 

After I submit the form I receive the below error in console from System.out.println(result);

Blockquoteorg.springframework.validation.BeanPropertyBindingResult: 1 errors Field error in object 'jmichango' on field 'kandaMchango': rejected value [41]; codes [typeMismatch.jmichango.kandaMchango,typeMismatch.kandaMchango,typeMismatch.com.church.church.entity.Kmichango,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [jmichango.kandaMchango,kandaMchango]; arguments []; default message [kandaMchango]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'com.church.church.entity.Kmichango' for property 'kandaMchango'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@javax.persistence.ManyToOne @javax.persistence.JoinColumn com.church.church.entity.Kmichango] for value '41'; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Provided id of the wrong type for class com.church.church.entity.Kmichango. Expected: class java.lang.Integer, got class java.lang.String; nested exception is java.lang.IllegalArgumentException: Provided id of the wrong type for class com.church.church.entity.Kmichango. Expected: class java.lang.Integer, got class java.lang.String]

The error is clear that it failed to convert the value type, Now How do i solve this and still maintain the relation of this two entity?

Ad

Answer

Inside jumuiya_michango_form.html you have a select

Please change that to name="kandaMchango.id", because you actually want to map the value of the selected option to the id of the kandaMchango object.

Ad
source: stackoverflow.com
Ad