6

Modelmapper is giving LazyInitializationException while converting from entity to dto.

Is there any way i can disable this. If am calling modelmapper.map inside transaction block it is working fine but it is loading all my lazy objects which i dont want at all. I want if lazy then do not load it at all.

Converter org.modelmapper.internal.converter.MergingCollectionConverter@6a51c12e failed to convert org.hibernate.collection.internal.PersistentSet to java.util.Set.

Caused by: org.modelmapper.MappingException: ModelMapper mapping errors:

1) Failed to get value from com.app.flashdiary.entity.Vendor.getApproved()

Caused by: org.hibernate.LazyInitializationException: could not initialize proxy [com.app.flashdiary.entity.Vendor#1] - no Session at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:169)

MartenCatcher
  • 2,713
  • 8
  • 26
  • 39
Nayak
  • 99
  • 1
  • 7
  • 1
    If you don't need those lazy loaded collections in DTO, simply instruct your mapper to not use those and you won't see more of `LazyInitializationException`s. – Piotr Podraza Jun 22 '19 at 13:14
  • can you please give me one simple example.am new to modelmapper and no idea how to skip as have a global mapper. – Nayak Jun 22 '19 at 13:30
  • 1
    I can't help you with modelMapper - haven't used it but I'm sure there must be a way to let it know that some properties should be skipped. You have to find the solution yourself. The simplest solution though would be to remove properties you don't need from the mapping destination object - the DTO you mentioned. Then you woud not need to instruct mapper to skip those properties as they have been removed. – Piotr Podraza Jun 22 '19 at 13:38

3 Answers3

8

I found the solution from here:

https://github.com/modelmapper/modelmapper/issues/97

modelMapper.getConfiguration().setPropertyCondition(new Condition<Object, Object>() {
      public boolean applies(MappingContext<Object, Object> context) {
        return !(context.getSource() instanceof PersistentCollection);
      }
    });
smile
  • 498
  • 7
  • 18
  • Ignoring something while mapping an object shouldn't be considered a solution. You wouldn't have the variable in the target object when it wouldn't be necessary right? – Renis1235 Feb 10 '22 at 21:14
1

JAVA_8

modelMapper.getConfiguration()
           .setPropertyCondition(context -> 
                 !(context.getSource() instanceof PersistentCollection)
            )
Marcus Voltolim
  • 413
  • 4
  • 12
1

I improved the @smile response, with his solution no PersistentCollection will be ever mapped. Instead with this solution the object that haven't LazyInitializationException will be mapped correctly:

    modelMapper.getConfiguration().setPropertyCondition(new Condition<Object, Object>() {
        public boolean applies(MappingContext<Object, Object> context) {
            //if the object is a PersistentCollection could be not initialized 
            //in case of lazy strategy, in this case the object will not be mapped:
            return (!(context.getSource() instanceof PersistentCollection) 
            || ((PersistentCollection)context.getSource()).wasInitialized());
        }
    });
Accollativo
  • 1,537
  • 4
  • 32
  • 56