javahibernatedozer

org.dozer.MappingException: java.lang.NoSuchMethodException: org.hibernate.internal.SessionImpl.<init>()


    @Override 
    public List<Order> findByOrderId(final Long orderId)
    {
        Criteria c = this.getHibernateSession().createCriteria(Order.class);
        c.createCriteria("context").add(Restrictions.eq("orderId", orderId));
        c.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        List<Order> list = c.list();
        return list;
    }

While mapping data from above received order entity to order vo using dozerMapper.

Consider a for loop here.
OrderVO orderVO = this.dozerMapper.map(order, OrderVO.class);

Getting below error:

org.dozer.MappingException: java.lang.NoSuchMethodException: org.hibernate.internal.SessionImpl.<init>()
at org.dozer.util.MappingUtils.throwMappingException(MappingUtils.java:82)
at org.dozer.factory.ConstructionStrategies$ByConstructor.newInstance(ConstructionStrategies.java:261)
at org.dozer.factory.ConstructionStrategies$ByConstructor.create(ConstructionStrategies.java:245)
at org.dozer.factory.DestBeanCreator.create(DestBeanCreator.java:65)
at org.dozer.MappingProcessor.mapCustomObject(MappingProcessor.java:477)
at org.dozer.MappingProcessor.mapOrRecurseObject(MappingProcessor.java:434)
at org.dozer.MappingProcessor.mapFromFieldMap(MappingProcessor.java:330)
at org.dozer.MappingProcessor.mapField(MappingProcessor.java:276)
at org.dozer.MappingProcessor.map(MappingProcessor.java:245)
at org.dozer.MappingProcessor.processSuperTypeMapping(MappingProcessor.java:999)
at org.dozer.MappingProcessor.map(MappingProcessor.java:234)
at org.dozer.MappingProcessor.mapCustomObject(MappingProcessor.java:483)
at org.dozer.MappingProcessor.mapOrRecurseObject(MappingProcessor.java:434)
at org.dozer.MappingProcessor.mapFromFieldMap(MappingProcessor.java:330)
at org.dozer.MappingProcessor.mapField(MappingProcessor.java:276)
at org.dozer.MappingProcessor.map(MappingProcessor.java:245)
at org.dozer.MappingProcessor.processSuperTypeMapping(MappingProcessor.java:999)
at org.dozer.MappingProcessor.map(MappingProcessor.java:234)
at org.dozer.MappingProcessor.map(MappingProcessor.java:187)
at org.dozer.MappingProcessor.map(MappingProcessor.java:124)
at org.dozer.MappingProcessor.map(MappingProcessor.java:119)
at org.dozer.DozerBeanMapper.map(DozerBeanMapper.java:111)

I'm not getting why dozer is throwing exception on org.hibernate.internal.SessionImpl class. Please let me know if more details needed.


Solution

  • Your OrderVO is probably referencing EntityManager or Session and Dozer tries to instantiate an implementation of that interface while mapping the entity. I don't know what kind of mapping you want to use here, but I think this is a perfect use case for Blaze-Persistence Entity Views.

    I created the library to allow easy mapping between JPA models and custom interface or abstract class defined models, something like Spring Data Projections on steroids. The idea is that you define your target structure(domain model) the way you like and map attributes(getters) via JPQL expressions to the entity model.

    A DTO model for your use case could look like the following with Blaze-Persistence Entity-Views:

    @EntityView(Order.class)
    public interface OrderVO {
        @IdMapping
        Long getId();
        @Mapping("customer.name")
        String getCustomerName();
        Set<OrderPositionVO> getPosition();
    
        @EntityView(OrderPosition.class)
        interface OrderPositionVO {
            @IdMapping
            Long getId();
            @Mapping("item.name")
            String getItemName();
            BigDecimal getAmount();
        }
    }
    

    Querying is a matter of applying the entity view to a query, the simplest being just a query by id.

    OrderVO a = entityViewManager.find(entityManager, OrderVO.class, id);

    The Spring Data integration allows you to use it almost like Spring Data Projections: https://persistence.blazebit.com/documentation/entity-view/manual/en_US/index.html#spring-data-features

    Page<OrderVO> findAll(Pageable pageable);
    

    The best part is, it will only fetch the state that is actually necessary!