jmapper-core icon indicating copy to clipboard operation
jmapper-core copied to clipboard

How to use custom object factory

Open exports opened this issue 8 years ago • 3 comments

Hi there!

Recently I'm trying to switch my MapStruct based bean mappers to JMapper, and I'm just wondering how can I set factories for object like the @ObjectFactory do in MapStruct? I'm trying to create mappers for my spring-data-jpa entity dtos, when map dto to actual entity, I want to load the entity by id from database instead of initialize a new instance.

exports avatar Apr 24 '17 04:04 exports

Hi @exports,

The request is not clear, could you write a sample code?

avurro avatar Apr 24 '17 08:04 avurro

@Entity class Foo {
  @Id private String id;
  @ManyToOne Bar bar;
}

@Entity class Bar {
  @Id private String id;
  @OneToMany Set<Bar> bars;
}

class FooDto {
  private String id;
  private BarDto bar;
}

class BarDto {
  private String id;
}

Both entities persisted in the database. When convert FooDto to Foo, I want to load the entity by id from database like entityManager.find(fooDto.getId(), Foo.class) or return a new instance if the entity not exists in database yet. In MapStruct 1.2.0.Beta, I can create a single ObjectFactory to manualy map a FooDto to Foo (as well as other entities) like this

@Component
public class PersistenceResolver {

  private EntityManager entityManager;

  public PersistenceResolver(EntityManager entityManager) {
    this.entityManager = entityManager;
  }

  @ObjectFactory
  public <T> T resolve(Resource resource, @TargetType Class<T> clazz) {
    String id = resource.getId();
    T entity = null;

    if (id == null) {
      try {
        entity = clazz.newInstance();
      } catch (IllegalAccessException | InstantiationException ignored) {
      }
    } else {
      entity = entityManager.find(clazz, id);
    }

    return entity;
  }
}

When converting dtos, the object mapper will call the ObjectFactory to extract a new instance of the given type instead of initialize a new one.

exports avatar Apr 25 '17 04:04 exports

Hi @exports,

Currently there isn't a similar feature, will surely be included in the list of upcoming developments. You may probably find this page useful: https://github.com/jmapper-framework/jmapper-core/wiki/Immutable-Objects

avurro avatar Apr 26 '17 08:04 avurro