2

我正在尝试将 DTO 映射到其对应的 Hibernate 实体。我从 Hibernate 会话加载实体并立即执行映射。

这是我得到的例外:

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)

这是在我的工厂构建器上配置的 HibernateUnenhanceStrategy,这就是我的 Mapper 的样子:

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);
    }
}

我现在的解决方法是在从会话加载和使用 Orika 映射之间自行取消对实体的增强,但我真的不想这么明确。我怎样才能得到这个工作?

4

1 回答 1

1

尽管这个问题已经很老了,但这是我的解决方案,以防有人遇到同样的问题。

似乎 HibernateUnenhancerStrategy 未应用于目标对象(代理)。

但是,有一个简单的解决方法。MapperFacade 展示了一个重写的 map() 方法,该方法允许传递目标类型:

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

所以你的电话看起来类似于:

mapperFacade.map(myDto, myProxiedEntity,
             null, TypeFactory.valueOf(MyEntity.class));
于 2015-08-17T10:54:43.890 回答