modelmapper / modelmapper

Intelligent object mapping
http://modelmapper.org
Apache License 2.0
2.28k stars 347 forks source link

Cannot detect Hibernate proxy classes #442

Closed kbytesys closed 5 years ago

kbytesys commented 5 years ago

If I add a mapping for Hibernate Entity to a DTO class convertion everything works well because the model mapper doesn't receive a proxied class.

But if the mapper receive a collection of objects that contain that Entity class, the Model mapper receives the proxied class. The result is that modelmapper doesn't match the type inside the configuration and misses the correct mapper.

Example: it.itsright.commondata.dal.entity.TypeOfRightEntity$HibernateProxy$cbqs6eEb

Looking at the deProxy method I see that isProxy doesn't detect any HibernateProxy class.

Is this a bug or I need to change something in my code?

// On the mapper init
modelMapper.createTypeMap(TypeOfRightEntity.class, TypeOfRight.class)
                .addMappings(mapper -> mapper.using(ctx -> ctx.getSource() == null ? null : ((TypeOfRightEntity)ctx.getSource()).getId())
                .map(TypeOfRightEntity::getRightOfAggregation, TypeOfRight::setRightOfAggregation));
modelMapper.getConfiguration()
                .setMatchingStrategy(MatchingStrategies.STRICT).setFieldMatchingEnabled(true);

// On the dto class (Informe83 contains a field wity the TypeOfRightEntity type
Page<Informe83Entity> p = someFunctionThatGetDataFromHibernate()
Type listType = new TypeToken<List<Informe83>>() { }.getType();

return new PageImpl<>(modelMapper.map(p.getContent(), listType), PageRequest.of(page, size), p.getTotalElements());

The strange thing is that if I use this (manual) conversion everything works fine!

        List<Informe83> test = new ArrayList<>();
        for (Informe83Entity item: p.getContent()) {
            test.add(modelMapper.map(item, Informe83.class));
        }
MrGraversen commented 5 years ago

Hello @kbytesys, my fix for this issue was merged.

@chhsiao90 – when do you expect a new release of ModelMapper to be drafted?

chhsiao90 commented 5 years ago

v2.3.3 is already released for a while!