Orika映射到Hibernate实体会引发异常

时间:2014-02-14 16:01:47

标签: java hibernate mapping dto orika

我正在尝试将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之间对自己的实体进行解除,但我真的不想明确这一点。我怎样才能使这个工作?

1 个答案:

答案 0 :(得分: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));