DEV Community

Adrian Matei for Codever

Posted on • Edited on • Originally published at codever.land

4 1

Force null value check on field mapping in Mapstruct

Use the nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS on the mapping property you want to be checked. In the following snippet you can see a PostalAdress entity is mapped to an entity of the same type, but the id is overwritten only when it is present in the source:

import java.time.LocalDateTime;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.NullValueCheckStrategy;

import com.example.entity.PostalAddress;

@Mapper(
    componentModel = "cdi",
    imports = {LocalDateTime.class})
public interface PostalAddressEntity2EntityMapperService {

  @Mapping(source = "id", target = "id", nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS)
  PostalAddress toPostalAddressntityToPostalAddressEntity(
      PostalAddress PostalAddress);
}
Enter fullscreen mode Exit fullscreen mode

This will generate something similar with the following

package com.example.entitytoentity;

import com.example.entity.PostalAddress;
import java.time.LocalDateTime;
import javax.annotation.processing.Generated;
import javax.enterprise.context.ApplicationScoped;

@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2021-09-01T14:28:11+0200",
    comments = "version: 1.4.2.Final, compiler: javac, environment: Java 11.0.12 (Azul Systems, Inc.)"
)
@ApplicationScoped
public class PostalAddressEntity2EntityMapperServiceImpl implements PostalAddressEntity2EntityMapperService {

    @Override
    public PostalAddress toPostalAddressEntity(PostalAddress PostalAddress) {
        if ( PostalAddress == null ) {
            return null;
        }

        PostalAddress PostalAddress1 = new PostalAddress();

        if ( PostalAddress.getId() != null ) {
            PostalAddress1.setId( PostalAddress.getId() );
        }
        PostalAddress1.setStreeNo( PostalAddress.getStreetNo() );
        PostalAddress1.setStreet( PostalAddress.getStreet() );
        PostalAddress1.setMainAdress( PostalAddress.getMainAdress() );

        return PostalAddress1;
    }
}

Enter fullscreen mode Exit fullscreen mode

Shared with ❤️ from Codever.   👉 use the copy to mine functionality to add it to your personal snippets collection.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay