From ff62723f3e45eaa9bf0063d427d9afd16fa2a75c Mon Sep 17 00:00:00 2001 From: mikereiche Date: Thu, 11 May 2023 11:11:09 -0700 Subject: [PATCH] Try Object.class if cannot get persistent entity for class. Closes #1726. --- .../convert/MappingCouchbaseConverter.java | 12 +++++++- ...ouchbaseTemplateQueryIntegrationTests.java | 26 +++++++++++++++++ .../data/couchbase/domain/PersonWithMaps.java | 29 +++++++++++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/springframework/data/couchbase/domain/PersonWithMaps.java diff --git a/src/main/java/org/springframework/data/couchbase/core/convert/MappingCouchbaseConverter.java b/src/main/java/org/springframework/data/couchbase/core/convert/MappingCouchbaseConverter.java index 819e34a74..0a34ce93b 100644 --- a/src/main/java/org/springframework/data/couchbase/core/convert/MappingCouchbaseConverter.java +++ b/src/main/java/org/springframework/data/couchbase/core/convert/MappingCouchbaseConverter.java @@ -20,6 +20,7 @@ import static org.springframework.data.couchbase.core.mapping.id.GenerationStrategy.USE_ATTRIBUTES; import java.beans.Transient; +import java.lang.reflect.InaccessibleObjectException; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -982,7 +983,16 @@ public R readValue(Object value, CouchbasePersistentProperty prop, Object pa private ConvertingPropertyAccessor getPropertyAccessor(Object source) { - CouchbasePersistentEntity entity = mappingContext.getRequiredPersistentEntity(source.getClass()); + CouchbasePersistentEntity entity = null; + try { + entity = mappingContext.getRequiredPersistentEntity(source.getClass()); + } catch(InaccessibleObjectException e){ + try { // punt + entity = mappingContext.getRequiredPersistentEntity(Object.class); + } catch(Exception ee){ + throw e; + } + } PersistentPropertyAccessor accessor = entity.getPropertyAccessor(source); return new ConvertingPropertyAccessor<>(accessor, conversionService); diff --git a/src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateQueryIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateQueryIntegrationTests.java index 7caa04a75..07b67fea2 100644 --- a/src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateQueryIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateQueryIntegrationTests.java @@ -26,8 +26,12 @@ import java.time.Instant; import java.time.temporal.TemporalAccessor; import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Locale; +import java.util.Map; +import java.util.Set; import java.util.UUID; import java.util.stream.Collectors; @@ -42,6 +46,7 @@ import org.springframework.data.couchbase.domain.Config; import org.springframework.data.couchbase.domain.Course; import org.springframework.data.couchbase.domain.NaiveAuditorAware; +import org.springframework.data.couchbase.domain.PersonWithMaps; import org.springframework.data.couchbase.domain.Submission; import org.springframework.data.couchbase.domain.User; import org.springframework.data.couchbase.domain.UserJustLastName; @@ -127,6 +132,27 @@ void findByQueryAll() { } + + @Test + void findById() { + PersonWithMaps person1 = new PersonWithMaps(); + person1.setId(UUID.randomUUID().toString()); + Map> versions=new HashMap<>(); + Set versionSet = new HashSet<>(); + versionSet.add("1.0"); + versions.put("v",versionSet); + person1.setVersions(versions); + Map> releaseVersions = new HashMap<>(); + Map releaseMap = new HashMap<>(); + releaseMap.put("1","1"); + releaseVersions.put("1",releaseMap); + person1.setReleaseVersions(releaseVersions); + couchbaseTemplate.upsertById(PersonWithMaps.class).one(person1); + PersonWithMaps person2 = couchbaseTemplate.findById(PersonWithMaps.class).one(person1.getId()); + assertEquals(person1, person2); + couchbaseTemplate.removeById(PersonWithMaps.class).oneEntity(person1); + } + @Test void findByMatchingQuery() { User user1 = new User(UUID.randomUUID().toString(), "user1", "user1"); diff --git a/src/test/java/org/springframework/data/couchbase/domain/PersonWithMaps.java b/src/test/java/org/springframework/data/couchbase/domain/PersonWithMaps.java new file mode 100644 index 000000000..87b5cb2ed --- /dev/null +++ b/src/test/java/org/springframework/data/couchbase/domain/PersonWithMaps.java @@ -0,0 +1,29 @@ +package org.springframework.data.couchbase.domain; + +import com.couchbase.client.core.deps.com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.Data; +import org.springframework.data.annotation.Id; +import org.springframework.data.couchbase.core.mapping.Document; +import org.springframework.data.couchbase.core.mapping.Field; + +import java.util.Map; +import java.util.Set; + +@Data +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(JsonInclude.Include.NON_NULL) +@Document +public class PersonWithMaps { + + @Id + private String id; + @Field + private Map> versions; + @Field + private Map> releaseVersions; + + public PersonWithMaps(){ + + } +}