简体   繁体   中英

Orika mapping to Hibernate entity throws exception

I'm trying to map a DTO to its counterpart, a Hibernate entity. I load the entity from the Hibernate session and perform the mapping straightaway.

This is the exception I get:

javassist.NotFoundException: my.pkg.MyEntity$$EnhancerByCGLIB$$a7950497
     at javassist.ClassPool.get(ClassPool.java:436)
     at ma.glasnost.orika.impl.generator.JavassistCompilerStrategy.assureTypeIsAccessible(JavassistCompilerStrategy.java:181)
     at ma.glasnost.orika.impl.generator.MapperGenerator.build(MapperGenerator.java:56)
     at ma.glasnost.orika.impl.DefaultMapperFactory.buildMapper(DefaultMapperFactory.java:1057)
     at ma.glasnost.orika.impl.DefaultMapperFactory.lookupMapper(DefaultMapperFactory.java:599)
     at ma.glasnost.orika.impl.DefaultMapperFactory.lookupMapper(DefaultMapperFactory.java:566)
     at ma.glasnost.orika.impl.MapperFacadeImpl.resolveMapper(MapperFacadeImpl.java:516)
     at ma.glasnost.orika.impl.MapperFacadeImpl.resolveMappingStrategy(MapperFacadeImpl.java:199)
     at ma.glasnost.orika.impl.MapperFacadeImpl.map(MapperFacadeImpl.java:337)
     at ma.glasnost.orika.impl.MapperFacadeImpl.map(MapperFacadeImpl.java:357)
     at ma.glasnost.orika.impl.ConfigurableMapper.map(ConfigurableMapper.java:148)

This is with the HibernateUnenhanceStrategy configured on my factory builder, this is what my Mapper looks like:

public class MyMapper extends ConfigurableMapper
{
    @Override
    protected void configureFactoryBuilder(DefaultMapperFactory.Builder factoryBuilder)
    {
        factoryBuilder.compilerStrategy(new JavassistCompilerStrategy());
        factoryBuilder.unenhanceStrategy(new HibernateUnenhanceStrategy());
    }

    @Override
    protected void configure(MapperFactory factory)
    {
        factory.classMap(MyEntity.class, MyDTO.class);
    }
}

My workaround right now is to unenhance to the entity myself, between loading from the session and mapping with Orika, but I really don't want to be that explicit about this. How can I get this working?

Although the question is quite old, here is my solution in case there is someone out there who ran into the same issue.

It seems that the HibernateUnenhancerStrategy is not applied on the destination object (the proxy).

However, there is an easy workaround. The MapperFacade exhibits an overridden map() method thats allows for passing the destination type:

    <S, D> void map(S sourceObject, D destinationObject, Type<S> sourceType, Type<D> destinationType);

So your call would look similar to:

mapperFacade.map(myDto, myProxiedEntity,
             null, TypeFactory.valueOf(MyEntity.class));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM