Thursday 10 March 2022

Dozer: Querying mapping metadata

You can query mapping metadata by getting an instance of ‘MappingMetadata’ interface.

 

Example

MappingMetadata mapMetadata = beanMapper.getMappingMetadata();

  Below snippet is used to get the field mapping information of ‘fName’ property.

 

ClassMappingMetadata classMappingMetadata = mapMetadata.getClassMapping(User.class, Student.class);
FieldMappingMetadata fieldMetadata = classMappingMetadata.getFieldMappingBySource("fName");
 


Find the below working application.

 

User.java

package com.sample.app.model;

public class User {

	private Integer id;
	private String fName;
	private String lName;

	public User() {
	}

	public User(Integer id, String fName, String lName) {
		this.id = id;
		this.fName = fName;
		this.lName = lName;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getfName() {
		return fName;
	}

	public void setfName(String fName) {
		this.fName = fName;
	}

	public String getlName() {
		return lName;
	}

	public void setlName(String lName) {
		this.lName = lName;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", fName=" + fName + ", lName=" + lName + "]";
	}

}

 

Student.java

package com.sample.app.model;

import org.dozer.Mapping;

public class Student {
	private Integer id;

	@Mapping("fName")
	private String firstName;

	@Mapping("lName")
	private String lastName;

	public Student() {
	}

	public Student(Integer id, String firstName, String lastName) {
		this.id = id;
		this.firstName = firstName;
		this.lastName = lastName;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getFirstName() {
		return firstName;
	}

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

	public String getLastName() {
		return lastName;
	}

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

	@Override
	public String toString() {
		return "Student [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
	}

}

 

QueryingMetadata.java

package com.sample.app;

import java.io.IOException;

import org.dozer.DozerBeanMapper;
import org.dozer.metadata.ClassMappingMetadata;
import org.dozer.metadata.FieldMappingMetadata;
import org.dozer.metadata.MappingMetadata;

import com.sample.app.model.Student;
import com.sample.app.model.User;

public class QueryingMetadata {

	public static void main(String[] args) throws IOException {

		DozerBeanMapper beanMapper = new DozerBeanMapper();
		beanMapper.map(new User(1,  "Arjun", "Gurram"), Student.class);

		MappingMetadata mapMetadata = beanMapper.getMappingMetadata();

		ClassMappingMetadata classMappingMetadata = mapMetadata.getClassMapping(User.class, Student.class);

		FieldMappingMetadata fieldMetadata = classMappingMetadata.getFieldMappingBySource("fName");

		System.out.println("fName mapped to : " + fieldMetadata.getDestinationName());

	}

}

 

Output

fName mapped to : firstName

 

 

 

 

 


  

Previous                                                 Next                                                 Home

No comments:

Post a Comment