kshubham07
kshubham07

Reputation: 391

Mapstruct between objects having exactly same fields

I have been using mapstruct to Map objects of classes which vary slightly.

Now, I have a usecase where the two classes are exactly same. One of the classes is a BO (Qualification) and the other is a DTO (QualificationRecord) having exactly same fields.

How can I use a @Mapper to convert between these two types?

So far, I am doing

@Mapping(source = "qualificationId", target = "qualificationId")
QualificationRecord getQualificationRecordFromQualification(final Qualification qualification);

And it is able to generate the mapper, setting all the fields. But, source = "qualificationId", target = "qualificationId" seems redundant and I had to add it only because there was no parameter-less @Mapping() annotation available.

Is there a way to tell the Mapper to copy all the fields, without writing one redundant line?

Upvotes: 3

Views: 4979

Answers (1)

michaeak
michaeak

Reputation: 1669

Just define mapping methods in an interface like this will copy all fields from one object to the other:

/**
     * Mapper. Automatically implemented by mapstruct.
 * 
 */
@Mapper
public interface SomeObjMapper {

    /**
     * instance.
     */
    final SomeObjMapper INSTANCE = Mappers.getMapper(SomeObjMapper.class);

    /**
     * Mapper method to map entity to domain. Automatically implemented by mapstruct.
     * 
     * @param entity
     *        given entity.

     * @return Returns the domain object.
     */
    SomeObj entityToDomain(SomeObjEntity entity);

    /**
     * Mapper method to map domain object to entity. Automatically implemented by mapstruct.
     * 
     * @param domain
     *        given domain object.
     * @return Returns the entity.
     */
    SomeObjEntity domainToEntity(SomeObj domain);

}

Upvotes: 6

Related Questions