Dozer Mapping Example

I am using BeanUtils for copying the Form Object Properties to Entitie Object

But when Copying the String format to Date format i am getting the Problem.

So,Particularly for that Problem i am use the different names in the Form and Entities for Date data types.

Now i am using Dozer,It is simply Excellent features...It provided good solution for date copying and other related issues also.it provide the all the features by using the one xml file.If the names are diffeent it is also provedie the solutions for that.

The jars are added according to the appache dozer official site
http://dozer.sourceforge.net/

the following is the example for that dozer mapping.



package com.test1;

import java.sql.Date;

public class Employee {
   
    private String name;
    private String eno;
    private String address;
    private Date dateOfBirth;
   
    public Date getDateOfBirth() {
        return dateOfBirth;
    }
    public void setDateOfBirth(Date dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public String getEno() {
        return eno;
    }
    public void setEno(String eno) {
        this.eno = eno;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
 

}























package com.test1;

public class Student {
   
    private String name;
    private int sno;
    private String address;
    private String dateOfBirth;
   
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getSno() {
        return sno;
    }
    public void setSno(int sno) {
        this.sno = sno;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getDateOfBirth() {
        return dateOfBirth;
    }
    public void setDateOfBirth(String dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }
   

}





The above two are two objects i am assigning the properties values in Student to Employee




public class DozerTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
       
        DozerBeanMapper mapper = new DozerBeanMapper(Arrays.asList(new String[]{"dozerBeanMapping.xml"}));
        System.out.println("sj");
        Student student = new Student();
        student.setName("ramu");
        student.setSno(5);
        student.setDateOfBirth("10/10/2012");
        Employee destObject = mapper.map(student, Employee.class);
        System.out.println("name :   "+destObject.getName());
        System.out.println("no   :   "+destObject.getEno());
        System.out.println("no   :   "+destObject.getDateOfBirth());
       

    }

}


The following is the dozer mapping xml file






<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozer.sourceforge.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://dozer.sourceforge.net
          http://dozer.sourceforge.net/schema/beanmapping.xsd">

    <configuration>
    <date-format>MM/dd/yyyy</date-format>
  </configuration>
         
    <mapping>
        <class-a>com.test1.Student</class-a>
        <class-b>com.test1.Employee</class-b>
    <field>
      <a>dateOfBirth</a>
      <b>dateOfBirth</b>
    </field>
    <field>
      <a>sno</a>
      <b>eno</b>
    </field>
    </mapping>
   
</mappings>









the O/P for the above Program is :


name :   ramu
no   :   5
no   :   2012-10-10









2 Responses to "Dozer Mapping Example"

  1. getting below error

    what to do??

    org.apache.commons.lang.StringUtils.contains(Ljava/lang/String;Ljava/lang/String;)Z

    ReplyDelete
    Replies
    1. Can you provide list of jars using for this example.I think , problem is there in jars .

      Delete