try getReference instead of find in Mapper

This commit is contained in:
Michael Hoennig 2024-09-26 06:31:57 +02:00
parent 08bce3d911
commit 7c46fdb36d
2 changed files with 4 additions and 5 deletions

View File

@ -87,8 +87,7 @@ abstract class Mapper extends ModelMapper {
} }
public <E> E fetchEntity(final String propertyName, final Class<E> entityClass, final Object subEntityUuid) { public <E> E fetchEntity(final String propertyName, final Class<E> entityClass, final Object subEntityUuid) {
// using getReference would be more efficient, but results in very technical error messages final var entity = em.getReference(entityClass, subEntityUuid);
final var entity = em.find(entityClass, subEntityUuid); // FIXME: try getReference
if (entity != null) { if (entity != null) {
return entity; return entity;
} }

View File

@ -50,7 +50,7 @@ class MapperUnitTest {
@Test @Test
void mapsBeanWithExistingSubEntity() { void mapsBeanWithExistingSubEntity() {
final SourceBean givenSource = SourceBean.builder().a("1234").b("Text").s1(new SubSourceBean1(GIVEN_UUID)).build(); final SourceBean givenSource = SourceBean.builder().a("1234").b("Text").s1(new SubSourceBean1(GIVEN_UUID)).build();
when(em.find(SubTargetBean1.class, GIVEN_UUID)).thenReturn(new SubTargetBean1(GIVEN_UUID, "xxx")); when(em.getReference(SubTargetBean1.class, GIVEN_UUID)).thenReturn(new SubTargetBean1(GIVEN_UUID, "xxx"));
final var result = mapper.map(givenSource, TargetBean.class); final var result = mapper.map(givenSource, TargetBean.class);
assertThat(result).usingRecursiveComparison().isEqualTo( assertThat(result).usingRecursiveComparison().isEqualTo(
@ -81,7 +81,7 @@ class MapperUnitTest {
@Test @Test
void mapsBeanWithSubEntityNotFound() { void mapsBeanWithSubEntityNotFound() {
final SourceBean givenSource = SourceBean.builder().a("1234").b("Text").s1(new SubSourceBean1(GIVEN_UUID)).build(); final SourceBean givenSource = SourceBean.builder().a("1234").b("Text").s1(new SubSourceBean1(GIVEN_UUID)).build();
when(em.find(SubTargetBean1.class, GIVEN_UUID)).thenReturn(null); when(em.getReference(SubTargetBean1.class, GIVEN_UUID)).thenReturn(null);
final var exception = catchThrowable(() -> final var exception = catchThrowable(() ->
mapper.map(givenSource, TargetBean.class) mapper.map(givenSource, TargetBean.class)
@ -94,7 +94,7 @@ class MapperUnitTest {
@Test @Test
void mapsBeanWithSubEntityNotFoundAndDisplayName() { void mapsBeanWithSubEntityNotFoundAndDisplayName() {
final SourceBean givenSource = SourceBean.builder().a("1234").b("Text").s2(new SubSourceBean2(GIVEN_UUID)).build(); final SourceBean givenSource = SourceBean.builder().a("1234").b("Text").s2(new SubSourceBean2(GIVEN_UUID)).build();
when(em.find(SubTargetBean2.class, GIVEN_UUID)).thenReturn(null); when(em.getReference(SubTargetBean2.class, GIVEN_UUID)).thenReturn(null);
final var exception = catchThrowable(() -> final var exception = catchThrowable(() ->
mapper.map(givenSource, TargetBean.class) mapper.map(givenSource, TargetBean.class)