April 11, 2010

Java Object Mapping - Dozer Example

« Mockito Example | Main | How to increase test coverage with Cobertura »

Central point of software development is to handle data aggregation, data separation, data transformation and so on. For example let's have a look at ESB (Enterprise Service Bus) software systems. The main aspect of ESBs is the orchestration of data from different endpoints. Often developers map directly objects via getter and setter methods. Another way is to use an automatic Java object mapping framework how Dozer. Dozer use a XML file, which contains attribute mapping instructions. Dozer object mapping based on Java bean specification. If Java attributes have the same name, they will be mapped by Dozer directly. If not, you have to extend the Dozer XMl file. For instance Dozer is used in Apache Camel project for transformation of data transfer objects.

1. Database object CustomerDBO:
@Entity
public class CustomerDBO {
    @Id
    private Long id;
    public void setId(Long id) {
        this.id = id;
    }
    public Long getId() {
        return this.id;
    }

    private String foreName;
    public void setForeName(String foreName) {
        this.forName = forName;
    }
    public String getForeName() {
        return this.forName;
    }

    private String sureName;
    public void setSureName(String sureName) {
        this.sureName = sureName;
    }
    public String getSureName() {
        return this.sureName;
    }

    private String sex;
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getSex() {
        return this.sex;
    }
}
2. Database Access object CustomerDBO:
public class CustomerDAO {

    private Long id;
    public void setId(Long id) {
        this.id = id;
    }
    public Long getId() {
        return this.id;
    }

    private String firstName;
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getFirstName() {
        return this.firstName;
    }

    private String lastName;
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getLastName() {
        return this.lastName;
    }

    private String gender;
    public void setGender(String gender) {
        this.gender = gender;
    }
    public String getGender() {
        return this.gender;
    }
}
3. Dozer mapping definition file dozer-bean-mappings.xml (bi-directional):

  
    org.developers.blog.dbo.CustomerDBO
    org.developers.blog.dbo.CustomerDTO
    
      foreName
      firstName
    
    
      sureName
      lastName
    
    
      sex
      gender
    
  

4. Spring Integration

    
        
            dozer-bean-mappings.xml
        
    

5. Usage
//get dbo object from persistence unit and map with Dozer
CustomerDAO dao = mapper.map(dbo, CustomerDAO.class);
Regards
Rafael Sobek

Technorati Tags:

Posted by rafael.sobek at 11:09 PM in Java

 

[Trackback URL for this entry]

Comment: Mohamed El-Beltagy at Di, 13 Apr 7:11 AM

We tried dozer before in one of our projects, around 3 years ago. We tried it among other object-to-object mappers. Dozer was very good till we got to nested objects. Most of the times, we had to use custom mappers and that's where things got nasty. So we replaced it with another mapper, which I don't recall right now.

Any way, may be it was because we were still evaluating Dozer and other O2O mappers and was short of time... :)

Comment: Karthik at Mo, 25 Okt 12:16 AM

I think the value in the class-b element in the xml file is wrong: It should reference the CustomerDAO and not CustomerDTO

Comment: kris at Do, 23 Dez 8:59 PM

Dude, I would make sure that my code works before posting anything. Except obvious error with the class name that someone already commented on, you also need to spell xsi:schemaLocation with capital L. I had to debug Dozer sources to fix this.

Your comment:

(not displayed)
 
 
 

Live Comment Preview:

 
« April »
SunMonTueWedThuFriSat
    123
45678910
11121314151617
18192021222324
252627282930