Orika lazy-loading when executing map method

Viewed 1248

I am using Orika to map my Hibernate entities to DTO objects in web service calls. These entities have @OneToMany and @ManyToOne relationships in them for parent-child relations. Additionally, I am using Spring JPA to create these entities.

When mapping a Hibernate entity to a POJO DTO class, the map() method causes the loading of all lazy-loaded attributes.

@Entity
@Table(name="folders")
public class FolderEntity {
    @Id
    @GeneratedValue
    private int id;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="consumerId")
    private ConsumerEntity consumer;

public class Folder
    private int id;
    private User consumer;

Folder folder = mapper.map(some_folder_entity, Folder.class);

In this case, the consumer attribute mapping causes Hibernate to load the child, which is not what I want.

I though the HibernateUnenhanceStrategy was meant to resolve this issue by removing the Hibernate proxy but it does not, or I am doing it wrong.

@Component
public class MapperFacadeFactory implements FactoryBean<MapperFacade> {

    public MapperFacade getObject() throws Exception {
        DefaultMapperFactory.Builder factoryBuilder = new DefaultMapperFactory.Builder();
        factoryBuilder.unenhanceStrategy(new HibernateUnenhanceStrategy());
        DefaultMapperFactory factory = factoryBuilder.build();      
        MapperFacade facade = factory.getMapperFacade();
        return facade;
    }

I realize I can exclude fields from the mapping configuration, but I want the children potentially in some web service calls but only want to high level parent attributes in other web service calls.

I believe I could create a CustomMapper to resolve this issue myself but before I go down that road I want to make sure I am not missing something built into Orika.

Thanks.

0 Answers
Related