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

ve7v8dk2  于 2021-07-18  发布在  Java
关注(0)|答案(1)|浏览(617)

我有两个实体使用一个主键相互引用,主键是一个实体中的整数。我不确定我是不是用赖特的方式做的。
下面是将主键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\u michango\u 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(result)收到以下错误;
blockquoteorg.springframework.validation.beanpropertybindingresult:1 errors字段“kandamchango”上对象“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'的属性值转换为属性'kandamchango'所需的类型'com.church.church.entity.kmichango';嵌套异常为org.springframework.core.convert.conversionfailedexception:未能将值“41”从类型[java.lang.string]转换为类型[@javax.persistence.manytoone@javax.persistence.joincolumn com.church.church.entity.kmichango];嵌套异常为org.springframework.dao.invaliddataaccessapiusageexception:为类com.church.church.entity.kmichango提供的id类型错误。应输入:class java.lang.integer,get class java.lang.string;嵌套异常为java.lang.illegalargumentexception:为类com.church.church.entity.kmichango提供的id类型错误。应为:class java.lang.integer,get class java.lang.string]
错误很明显,它无法转换值类型,现在我如何解决这个问题,仍然保持这两个实体的关系?

zy1mlcev

zy1mlcev1#

在jumuiya\u michango\u form.html中,您可以选择
请将其更改为name=“kandamchango.id”,因为您实际上想要将所选选项的值Map到kandamchango对象的id。

相关问题