title: Is there a way to specify a custom getter in a Mapstruct mapper for a single field?
body: <p>I'm trying to find a better solution for preventing hibernate proxy initialization when mapping an entity to a response DTO via MapStruct.</p>
  <p>I've been converting our codebase to use MapStruct from ModelMapper. If I wanted to accomplish what I'm asking with ModelMapper, I could do something simple like this:</p>
  <pre class="lang-java prettyprint-override"><code>    modelMapper
          .createTypeMap(Entity.class, DTO.class)
          .addMapping(Entity::customGetterMethod, Category::setNormalSetterHere);
  </code></pre>
  <p>That Custom getter method allows me to check if a field has already been fetched from the DB or not to avoid N+1 initializations.</p>
  <p>it looks like:</p>
  <pre class="lang-java prettyprint-override"><code> public Set&lt;Entity&gt; customGetterMethod() {
      return Hibernate.isInitialized(this.entities) ? this.entities : null;
    }
  </code></pre>
  <p>I cannot simply override the normal getter because there are reasonable cases when working with an entity that we would want to allow lazy initialization.</p>
  <p>I've tried overriding the default naming strategy to use my own custom getter naming, but since I still need to access normal getters for most basic fields, I couldn't get it to reliably use my customGetter and ignore the default getter when it exists ( and even if I could, its a still seems like a messy solution and difficult to bring teammates up to speed with).</p>
  <p>The solution so far has been to use an <code>expression</code> and just copy and modify the generated mapping code for these sorts of fields:</p>
  <pre class="lang-java prettyprint-override"><code>  @Mapping(
        target = &quot;entities&quot;,
        expression = &quot;java( mapEntities( source.customGetterMethod(), context ) )&quot;)
    public abstract ResponseDto toDto(Entity source, @Context CycleAvoidingContext context);
  
     protected Set&lt;ResponseDto&gt; mapEntities(Set&lt;Entity&gt; set, CycleAvoidingContext context) {
         /* a copy of the auto-generated code for this mapping essentially */
    }
  </code></pre>
  <p>With all the entities and relationships in our projects, this is hardly sustainable since I have to add in an annotated mapping with an expression string for each field along with custom (not custom, just copied) mapping logic. From a maintainability and documentation standpoint, it adds a lot of complexity.</p>
  <p>I'm hoping someone can point me to some other mapstruct functionality to accomplish using a custom getter for particular fields in a more streamlined way.</p>