当前位置: 首页 > 知识库问答 >
问题:

无法将类型“java.lang.String”的属性值转换为所需类型“[]”

傅穆冉
2023-03-14

我有两个实体,它们使用一个主键互相引用,主键是一个实体的整数。我不确定我做这件事的方式是否正确。

下面是引用主键id为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,.......}        

下面是我们从上面的实体中将外键设置为Kmichango kandaMchango的实体。

@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,.......}   

这里是表单的一部分,我在这里提交了用户在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>

下面是我的控制器中用于链接到表单和发布数据的两个方法

@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";
} 

在我提交表单后,我在控制台收到以下来自System.out.println(结果)的错误;

BlockQuoteOrg.SpringFramework.Validation.BeanPropertyBindingResult:1错误字段“Kanda MChango”上对象“JMichango”中的字段错误:拒绝值[41];代码[typeMismatch.jmichango.kandamchango,typeMismatch.kandamchango,typeMismatch.com.church.church.entity.kmichango,typeMismatch];参数[org.springframework.context.support.defaultmessageSourceResolvable:代码[jmichango.kandaMchango,kandaMchango];参数[];默认消息[kandaMchango]];默认消息[无法将“java.lang.String”类型的属性值转换为属性“Kanda MChango”所需的类型“com.church.church.Entity.Kmichango”;嵌套异常为org.springframework.core.convert.ConversionFailedException:无法将值“41”从类型[java.lang.String]转换为类型[@javax.persistence.Many转换为一个@javax.persistence.JoinColumn com.church.church.Entity.Kmichango];嵌套异常为

错误很明显,它未能转换值类型,现在我如何解决这个问题,并仍然保持这两个实体的关系?

共有1个答案

敖涵容
2023-03-14

在jumuiya_michango_form.html中,您有一个select

请将其更改为name=“kandaMchango.id”,因为您实际上希望将所选选项的值映射到kandaMchango对象的id。

 类似资料: