diff --git a/redis-om-spring/src/main/java/com/redis/om/spring/repository/support/SimpleRedisDocumentRepository.java b/redis-om-spring/src/main/java/com/redis/om/spring/repository/support/SimpleRedisDocumentRepository.java index f6f95ed94..df3b5c337 100644 --- a/redis-om-spring/src/main/java/com/redis/om/spring/repository/support/SimpleRedisDocumentRepository.java +++ b/redis-om-spring/src/main/java/com/redis/om/spring/repository/support/SimpleRedisDocumentRepository.java @@ -108,7 +108,6 @@ public SimpleRedisDocumentRepository( // @Override public Iterable getIds() { - List result = List.of(); Gson gson = gsonBuilder.create(); Optional maybeIdField = ObjectUtils.getIdFieldForEntityClass(metadata.getJavaType()); String idField = maybeIdField.map(Field::getName).orElse("id"); @@ -141,7 +140,7 @@ public void deleteById(ID id, Path path) { @Override public void updateField(T entity, MetamodelField field, Object value) { modulesOperations.opsForJSON().set(getKey(Objects.requireNonNull(metadata.getId(entity))), value, - Path.of("$." + field.getSearchAlias())); + Path.of("$." + field.getSearchAlias().replace("_", "."))); } @SuppressWarnings("unchecked") @@ -413,91 +412,4 @@ private SearchOperations getSearchOps() { return modulesOperations.opsForSearch(maybeSearchIndex.get()); } -// static class FluentQueryByExample implements FluentQuery.FetchableFluentQuery { -// private final SearchStream searchStream; -// private final Class probeType; -// -// private final SearchOperations searchOps; -// -// public FluentQueryByExample( // -// Example example, // -// Class probeType, // -// EntityStream entityStream, // -// SearchOperations searchOps // -// ) { -// this.probeType = probeType; -// this.searchOps = searchOps; -// this.searchStream = entityStream.of(probeType); -// searchStream.filter(example); -// } -// -// @Override -// public FetchableFluentQuery sortBy(Sort sort) { -// searchStream.sorted(sort); -// return this; -// } -// -// @Override -// public FetchableFluentQuery as(Class resultType) { -// throw new UnsupportedOperationException("`as` is not supported on a Redis Repositories"); -// } -// -// @Override -// public FetchableFluentQuery project(Collection properties) { -// List> metamodelFields = MetamodelUtils.getMetamodelFieldsForProperties(probeType, -// properties); -// metamodelFields.forEach(mmf -> searchStream.project((MetamodelField) mmf)); -// return this; -// } -// -// @Override -// public T oneValue() { -// var result = searchStream.collect(Collectors.toList()); -// -// if (org.springframework.util.ObjectUtils.isEmpty(result)) { -// return null; -// } -// -// if (result.size() > 1) { -// throw new IncorrectResultSizeDataAccessException("Query returned non unique result", 1); -// } -// -// return result.iterator().next(); -// } -// -// @Override -// public T firstValue() { -// return searchStream.findFirst().orElse(null); -// } -// -// @Override -// public List all() { -// return searchStream.collect(Collectors.toList()); -// } -// -// @Override -// public Page page(Pageable pageable) { -// Query query = (searchStream.backingQuery().isBlank()) ? new Query() : new Query(searchStream.backingQuery()); -// query.limit(0, 0); -// SearchResult searchResult = searchOps.search(query); -// var count = searchResult.getTotalResults(); -// var pageContents = searchStream.limit(pageable.getPageSize()).skip(pageable.getOffset()).collect(Collectors.toList()); -// return new PageImpl<>(pageContents, pageable, count); -// } -// -// @Override -// public Stream stream() { -// return all().stream(); -// } -// -// @Override -// public long count() { -// return searchStream.count(); -// } -// -// @Override -// public boolean exists() { -// return searchStream.count() > 0; -// } -// } } diff --git a/redis-om-spring/src/test/java/com/redis/om/spring/annotations/document/BasicRedisDocumentMappingTest.java b/redis-om-spring/src/test/java/com/redis/om/spring/annotations/document/BasicRedisDocumentMappingTest.java index eb881f19a..e6915d35c 100644 --- a/redis-om-spring/src/test/java/com/redis/om/spring/annotations/document/BasicRedisDocumentMappingTest.java +++ b/redis-om-spring/src/test/java/com/redis/om/spring/annotations/document/BasicRedisDocumentMappingTest.java @@ -39,10 +39,20 @@ class BasicRedisDocumentMappingTest extends AbstractBaseDocumentTest { @Autowired SomeDocumentRepository someDocumentRepository; + @Autowired + DeepNestRepository deepNestRepository; + @BeforeEach void cleanUp() { flushSearchIndexFor(Company.class); flushSearchIndexFor(DocWithSets.class); + + if (deepNestRepository.count() == 0) { + DeepNest dn1 = DeepNest.of("dn-1", NestLevel1.of("nl-1-1", "Louis, I think this is the beginning of a beautiful friendship.", NestLevel2.of("nl-2-1", "Here's looking at you, kid."))); + DeepNest dn2 = DeepNest.of("dn-2", NestLevel1.of("nl-1-2", "Whoever you are, I have always depended on the kindness of strangers.", NestLevel2.of("nl-2-2", "Hey, you hens! Cut out the cackling in there!"))); + DeepNest dn3 = DeepNest.of("dn-3", NestLevel1.of("nl-1-3", "A good body with a dull brain is as cheap as life itself.", NestLevel2.of("nl-2-3", "I'm Spartacus!"))); + deepNestRepository.saveAll(List.of(dn1, dn2, dn3)); + } } @Test @@ -623,4 +633,18 @@ void testOrderByInMethodName() { () -> assertThat(onlyVal3).containsExactly(doc3) // ); } + + @Test + void testUpdateDeepNestedField() { + Optional dn1 = deepNestRepository.findFirstByNameIs("dn-1"); + assertTrue(dn1.isPresent()); + deepNestRepository.updateField(dn1.get(), DeepNest$.NEST_LEVEL1_NEST_LEVEL2_NAME, "dos-uno"); + + Optional dn1After = deepNestRepository.findFirstByNameIs("dn-1"); + + assertAll( // + () -> assertTrue(dn1After.isPresent()), // + () -> assertEquals("dos-uno", dn1After.get().getNestLevel1().getNestLevel2().getName()) // + ); + } } diff --git a/redis-om-spring/src/test/java/com/redis/om/spring/annotations/document/fixtures/DeepNestRepository.java b/redis-om-spring/src/test/java/com/redis/om/spring/annotations/document/fixtures/DeepNestRepository.java index dd59f24fd..064638337 100644 --- a/redis-om-spring/src/test/java/com/redis/om/spring/annotations/document/fixtures/DeepNestRepository.java +++ b/redis-om-spring/src/test/java/com/redis/om/spring/annotations/document/fixtures/DeepNestRepository.java @@ -2,6 +2,8 @@ import com.redis.om.spring.repository.RedisDocumentRepository; -@SuppressWarnings("unused") public interface DeepNestRepository extends RedisDocumentRepository { +import java.util.Optional; +@SuppressWarnings("unused") public interface DeepNestRepository extends RedisDocumentRepository { + Optional findFirstByNameIs(String name); }