I am working on a project whose goal is to solve common JPA problems when matching objects with DTO using ModelMapper. This issue has already been resolved by the project. Project Link: JPA Model Mapper
"Its crucial to performance is to declare objects as lazy loading, so we don’t need to retrieve all related entities every time we need some data. But this technique leads to some problems. The most common is LazyInitializationException, which can be quite annoying sometimes. . Most of the time we just need a null object for an unloaded object instead of the object that throws an exception on access ... "
Source: JPA Model Mapper
Therefore, in the project we are dealing with a LazyInitializationException by setting null for all not loaded objects. The examples below show how this works.
Transferring an entity that sets null for all unloaded objects:
TypedQuery<SystemEntity> query = em.createQuery("select s from SystemEntity s where s.id = 1", SystemEntity.class); SystemEntity system = query.getSingleResult(); return new JpaModelMapper(em).mapEntity(system, SystemEntity.class);
Transferring an entity to the DTO parameter for all unloaded objects:
TypedQuery<SystemEntity> query = em.createQuery("select s from SystemEntity s where s.id = 1", SystemEntity.class); SystemEntity system = query.getSingleResult(); return new JpaModelMapper(em).mapEntity(system, SystemDTO.class);
See JPA Model Mapper for more information.
Vinícius M. Freitas Apr 17 '18 at 19:07 2018-04-17 19:07
source share