【问题标题】:Dozer mapping not working for nested object推土机映射不适用于嵌套对象
【发布时间】:2016-02-09 06:34:45
【问题描述】:

我试图将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>

控制器方法

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

【问题讨论】:

    标签: java spring-mvc nhibernate dozer


    【解决方案1】:

    您正在将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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-19
      • 1970-01-01
      • 1970-01-01
      • 2014-05-22
      相关资源
      最近更新 更多