2

If you've used JPA you've probably run into the classic

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: class, could not initialize proxy - no Session

I'm using ModelMapper to map my hibernate objects into DTOs and, of course, I'm running into this issue. I originally, up to this point, had everything set to EAGER but I have quickly learned that that is NOT the solution as everything is now EXTREMELY slow to load. So, now I'm back to setting things to LAZY.

So my question is: is there a way to customize the ModelMapper so that it can check if the field is lazy before mapping / and if it's lazy, don't map it?

I've been doing some research into custom mappings here http://modelmapper.org/user-manual/property-mapping/

but i can't seem to find a way to throw in some logic.

I found a solution here but this is for Dozer

EDIT:

I am using Spring Boot JPA. Here is some of my code:

@Data
@Entity
@Table(name = "vm_request")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class VirtualMachineRequest {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    // removed other properties

    @OneToMany(mappedBy = "request", cascade = CascadeType.ALL, orphanRemoval = true)
    @ToString.Exclude
    @EqualsAndHashCode.Exclude
    private Set<VirtualMachine> virtualMachines;

}

I am mapping this object to a DTO:

@Data
public class VirtualMachineRequestDTO {

    private Long id;

    // removed other properties

    private Set<VirtualMachineDTO> virtualMachines;

}

using ModelMapper as such:

public VirtualMachineRequestDTO convertToDTO(VirtualMachineRequest request) {
    VirtualMachineRequestDTO requestDTO = mapper.map(request, VirtualMachineRequestDTO.class);

    return requestDTO;
}

My problem is that since the virtualMachines set is Lazy by default (and this is intended for certain situations), ModelMapper encounters the exception LazyInitializationException

I'm trying to find a way to have ModelMapper ignore the field if it's lazy and hasn't been initialized.

AKrush95
  • 1,381
  • 6
  • 18
  • 35
  • First of all, can you share your code end to end for showing us how you use it? Do you use spring boot or what? – Emre Savcı Dec 20 '18 at 05:54
  • Fair point, i'll update accordingly – AKrush95 Dec 21 '18 at 14:14
  • Well, either you need the values of `virtualMachines` or you don't. If it depends on the context, why not simply create another `VirtualMachineRequestSummaryDTO` without the `virtualMachines` property? – crizzis Dec 21 '18 at 20:43
  • 1
    Did you try to mark your method chain with @Transactional annotation? It happens because you are achieve a lazy entity outside of session. – Emre Savcı Dec 21 '18 at 21:40
  • @EmreSavcı What I am looking for is a way to have ModelMapper ignore the lazy fields. I don't need those fields in this instance (I wouldn't mind if this list were to be empty in the DTO) but I need a way to not encounter `LazyInitializationException` every time – AKrush95 Dec 26 '18 at 19:30
  • @AKrush95 take a look at official doc: http://modelmapper.org/examples/flattening/ . Also there is a good tutorial here : https://amydegregorio.com/2018/08/02/deep-mapping-with-modelmapper/ – Emre Savcı Dec 27 '18 at 10:30

0 Answers0