JPA Model Mapper aims to solve common JPA mapping issues and also issues when mapping entities to DTOs using Model Mapper.
The LazyInitializationException issue:
Its crucial for performance to declare entities as lazy load so we don't need to fetch all related entities every time we need some data. But this technique leads to some issues. The most common one is the LazyInitializationException that can be pretty annoying sometimes. Most of the time we would just want a null object for a not loaded entity instead of an object that throws an exception if accessed. The examples below show how to solve the LazyInitializationException issue using JPA Model Mapper.
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);
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);
TypedQuery<SystemEntity> query =
em.createQuery("select s from SystemEntity s join fetch s.monitoring", SystemEntity.class);
List<SystemEntity> systems = query.getResultList();
return new JpaModelMapper(em).mapEntities(systems, SystemEntity.class);
TypedQuery<SystemEntity> query =
em.createQuery("select s from SystemEntity s join fetch s.monitoring", SystemEntity.class);
List<SystemEntity> systems = query.getResultList();
return new JpaModelMapper(em).mapEntities(systems, SystemDTO.class);
Additionally, the library comes with a MappingQuery feature which simplifies the process by mapping a query instead of its result. The examples below show this functionality.
TypedQuery<SystemEntity> query =
em.createQuery("select s from SystemEntity s where s.id = 1", SystemEntity.class);
return new JpaModelMapper(em).mapQuery(query, SystemEntity.class).getSingleResult();
The getSingleResult
in MappingQuery.java
method has an exclusive feature. It calls the entity manager's getSingleResult() method as expected but it does not throw the NoResultException if there is no result to return. Instead, it returns null if there is no result.
TypedQuery<SystemEntity> query =
em.createQuery("select s from SystemEntity s", SystemEntity.class);
return new JpaModelMapper(em).mapQuery(query, SystemEntity.class).getResultList();