8

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.

smmelzer
  • 230
  • 2
  • 10
  • What you want is what Blaze-Persistence Entity Views were made for: https://persistence.blazebit.com/documentation/entity-view/manual/en_US/index.html#first-entity-view-query – Christian Beikov Mar 12 '17 at 20:10

0 Answers0