From 7c46fdb36d7de35640c8f694595e09ab34d21e48 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Thu, 26 Sep 2024 06:31:57 +0200 Subject: [PATCH] try getReference instead of find in Mapper --- src/main/java/net/hostsharing/hsadminng/mapper/Mapper.java | 3 +-- .../net/hostsharing/hsadminng/rbac/test/MapperUnitTest.java | 6 +++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/hostsharing/hsadminng/mapper/Mapper.java b/src/main/java/net/hostsharing/hsadminng/mapper/Mapper.java index aabfdfd9..21779a5c 100644 --- a/src/main/java/net/hostsharing/hsadminng/mapper/Mapper.java +++ b/src/main/java/net/hostsharing/hsadminng/mapper/Mapper.java @@ -87,8 +87,7 @@ abstract class Mapper extends ModelMapper { } public E fetchEntity(final String propertyName, final Class entityClass, final Object subEntityUuid) { - // using getReference would be more efficient, but results in very technical error messages - final var entity = em.find(entityClass, subEntityUuid); // FIXME: try getReference + final var entity = em.getReference(entityClass, subEntityUuid); if (entity != null) { return entity; } diff --git a/src/test/java/net/hostsharing/hsadminng/rbac/test/MapperUnitTest.java b/src/test/java/net/hostsharing/hsadminng/rbac/test/MapperUnitTest.java index fa8823b3..b90c7cb1 100644 --- a/src/test/java/net/hostsharing/hsadminng/rbac/test/MapperUnitTest.java +++ b/src/test/java/net/hostsharing/hsadminng/rbac/test/MapperUnitTest.java @@ -50,7 +50,7 @@ class MapperUnitTest { @Test void mapsBeanWithExistingSubEntity() { 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); assertThat(result).usingRecursiveComparison().isEqualTo( @@ -81,7 +81,7 @@ class MapperUnitTest { @Test void mapsBeanWithSubEntityNotFound() { 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(() -> mapper.map(givenSource, TargetBean.class) @@ -94,7 +94,7 @@ class MapperUnitTest { @Test void mapsBeanWithSubEntityNotFoundAndDisplayName() { 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(() -> mapper.map(givenSource, TargetBean.class)