gpt4 book ai didi

java - 推土机映射不适用于嵌套对象

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:55:02 25 4
gpt4 key购买 nike

我正在尝试将 StudentRequestForm 类的值复制到 StudentEntity 类。所有值都已保存,但 studentBean 中的 classI 字段 未保存,它映射到 StudentEntity classId 。所以我想将我的 ClassI 值复制到 classId

您可以在下面的推土机映射中查看


public class StudentRequestForm {

private StudentModel studentbean;

public StudentModel getStudentbean() {
return studentbean;
}
public void setStudentbean(StudentModel studentbean) {
this.studentbean = studentbean;
}`enter code here`




public class StudentModel {

private int countryId,cityId,stateId,classI;
private String enrollmentId,firstName,lastName,gender,category,pincode,sectionName;



@Entity
@Table(name="students")
public class StudentEntity implements Serializable {
private static final long serialVersionUID = 1L;

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

private int age;

private String category;

private String pincode;

private int cityId;

@Column(name="class_id")
private int classId;

@Column(name="country_id")
private int countryId;



@Column(name="first_name")
private String firstName;

private String gender

...........................................;

推土机映射

<mapping>
<class-a>com.myschool.entity.StudentRequestForm</class-a>
<class-b>com.myschool.entity.StudentEntity</class-b>

<field>
<a>studentbean.classI</a>
<b>classId</b>
</field>


</mapping>

Controller 方法

public ModelAndView saveForm(HttpServletRequest request,HttpServletResponse response,StudentRequestForm studentForm){
try{


StudentEntity studententity = mapper.map(studentForm.getStudentbean(), StudentEntity.class);
studentservice.saveStudentForm(studententity);

}catch(Exception e){
e.printStackTrace();
}
return new ModelAndView("abc/helo.html");
}

最佳答案

您正在将 StudentModel 类传递给 Dozer,但在您的映射中,您正在使用 StudentRequestForm。所以,要么这样做

StudentEntity studententity = mapper.map(studentForm, StudentEntity.class);

    <class-a>com.myschool.entity.StudentModel</class-a>
<class-b>com.myschool.entity.StudentEntity</class-b>

<field>
<a>classI</a>
<b>classId</b>
</field>

关于java - 推土机映射不适用于嵌套对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35285399/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com