Sunday, 1 May 2016

How to convert entity into value object or bean using Model Mapper

Converting the mapper is as like below

TravelDetail travelDetail = new TravelDetail();
travelDetail.setId(123);
travelDetail.setPackage(4000);

ModelMapper mapperFromDto = new ModelMapper();
TravelDetailVO travelDetailVO = mapperFromDto.map(travelDetail, TravelDetailVO.class);

Entity is crated as like below

@Entity
@Table(name="TRAVEL_DETAIL", schema="TRAVEL")
@NamedQuery(name="TravelDetail.findAll", query="SELECT f FROM TravelDetail f")
public class TravelDetail implements Serializable {
private static final long serialVersionUID = 1L;

@Id
private long id;

private string package;

private BigDecimal price;

//get set methods
}


The bean or value object is created , the variables and its datatype should match with the variables and the datatype of entity

public class TravelDetailVO implements Serializable {
private static final long serialVersionUID = 1L;

private long id;

private string package;

private BigDecimal price;

//get set methods
}

Add the dependency given below in pom.xml

                 <!-- Model Mapper -->
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>0.7.4</version>
</dependency>

No comments:

Post a Comment