Refine search
@Test public void findsByExample() { Customer probe = new Customer(null, "Matthews"); List<Customer> result = repository.findAll(Example.of(probe)); assertThat(result).hasSize(2).extracting("firstName").contains("Dave", "Oliver August"); } }
/** * Returns the given {@link Example} as {@link Document} holding matching values extracted from * {@link Example#getProbe()}. * * @param example must not be {@literal null}. * @return */ public Document getMappedExample(Example<?> example) { Assert.notNull(example, "Example must not be null!"); return getMappedExample(example, mappingContext.getRequiredPersistentEntity(example.getProbeType())); }
/** * Extract the {@link Predicate} representing the {@link Example}. * * @param root must not be {@literal null}. * @param cb must not be {@literal null}. * @param example must not be {@literal null}. * @return never {@literal null}. */ public static <T> Predicate getPredicate(Root<T> root, CriteriaBuilder cb, Example<T> example) { Assert.notNull(root, "Root must not be null!"); Assert.notNull(cb, "CriteriaBuilder must not be null!"); Assert.notNull(example, "Example must not be null!"); ExampleMatcher matcher = example.getMatcher(); List<Predicate> predicates = getPredicates("", cb, root, root.getModel(), example.getProbe(), example.getProbeType(), new ExampleMatcherAccessor(matcher), new PathNode("root", null, example.getProbe())); if (predicates.isEmpty()) { return cb.isTrue(cb.literal(true)); } if (predicates.size() == 1) { return predicates.iterator().next(); } Predicate[] array = predicates.toArray(new Predicate[0]); return matcher.isAllMatching() ? cb.and(array) : cb.or(array); }
/** * Static factory method to create a {@link Criteria} matching an example object. * * @param example must not be {@literal null}. * @return * @see Criteria#alike(Example) * @since 1.8 */ public static Criteria byExample(Object example) { return byExample(Example.of(example)); }
/** * Retrieve a mapped {@link RedisOperationChain} to query secondary indexes given {@link Example}. * * @param example must not be {@literal null}. * @return the mapped {@link RedisOperationChain}. */ public RedisOperationChain getMappedExample(Example<?> example) { RedisOperationChain chain = new RedisOperationChain(); ExampleMatcherAccessor matcherAccessor = new ExampleMatcherAccessor(example.getMatcher()); applyPropertySpecs("", example.getProbe(), mappingContext.getRequiredPersistentEntity(example.getProbeType()), matcherAccessor, example.getMatcher().getMatchMode(), chain); return chain; }
private <T> void validateExample(Example<T> example) { Assert.notNull(example, "A non-null example is expected"); ExampleMatcher matcher = example.getMatcher(); if (!matcher.isAllMatching()) { throw new DatastoreDataException("Unsupported MatchMode. Only MatchMode.ALL is supported"); } if (matcher.isIgnoreCaseEnabled()) { throw new DatastoreDataException("Ignore case matching is not supported"); } if (!(matcher.getDefaultStringMatcher() == ExampleMatcher.StringMatcher.EXACT || matcher.getDefaultStringMatcher() == ExampleMatcher.StringMatcher.DEFAULT)) { throw new DatastoreDataException("Unsupported StringMatcher. Only EXACT and DEFAULT are supported"); } Optional<String> path = example.getMatcher().getIgnoredPaths().stream().filter((s) -> s.contains(".")).findFirst(); if (path.isPresent()) { throw new DatastoreDataException("Ignored paths deeper than 1 are not supported"); } if (matcher.getPropertySpecifiers().hasValues()) { throw new DatastoreDataException("Property matchers are not supported"); } } }
public String convertExampleToPredicate(final Example<T> example, final Map<String, Object> bindVars) { final StringBuilder predicateBuilder = new StringBuilder(); final ArangoPersistentEntity<?> persistentEntity = context.getPersistentEntity(example.getProbeType()); Assert.isTrue(example.getProbe() != null, "Probe in Example cannot be null"); traversePropertyTree(example, predicateBuilder, bindVars, "", "", persistentEntity, example.getProbe()); return predicateBuilder.toString(); }
@Override public <S extends T> List<S> findAll(Example<S> example, Sort sort) { return getQuery(new ExampleSpecification<S>(example), example.getProbeType(), sort).getResultList(); }
switch (example.getMatcher().getDefaultStringMatcher()) { case EXACT: likeType = LikeType.EQUAL_TO; break; return ebeanServer.getExpressionFactory().exampleLike(example.getProbe(), example.getMatcher().isIgnoreCaseEnabled(), likeType);
private boolean isTypeRestricting(Example example) { if (example.getMatcher() instanceof UntypedExampleMatcher) { return false; } if (example.getMatcher().getIgnoredPaths().isEmpty()) { return true; } for (String path : example.getMatcher().getIgnoredPaths()) { if (this.converter.getTypeMapper().isTypeKey(path)) { return false; } } return true; }
/** * Get the actual type for the probe used. This is usually the given class, but the original class in case of a * CGLIB-generated subclass. * * @return * @see ProxyUtils#getUserClass(Class) */ @SuppressWarnings("unchecked") default Class<T> getProbeType() { return (Class<T>) ProxyUtils.getUserClass(getProbe().getClass()); } }
public String convertExampleToPredicate(final Example<T> example, final Map<String, Object> bindVars) { final StringBuilder predicateBuilder = new StringBuilder(); final ArangoPersistentEntity<?> persistentEntity = context.getPersistentEntity(example.getProbeType()); Assert.isTrue(example.getProbe() != null, "Probe in Example cannot be null"); traversePropertyTree(example, predicateBuilder, bindVars, "", "", persistentEntity, example.getProbe()); return predicateBuilder.toString(); }
/** * Retrieve a mapped {@link RedisOperationChain} to query secondary indexes given {@link Example}. * * @param example must not be {@literal null}. * @return the mapped {@link RedisOperationChain}. */ public RedisOperationChain getMappedExample(Example<?> example) { RedisOperationChain chain = new RedisOperationChain(); ExampleMatcherAccessor matcherAccessor = new ExampleMatcherAccessor(example.getMatcher()); applyPropertySpecs("", example.getProbe(), mappingContext.getRequiredPersistentEntity(example.getProbeType()), matcherAccessor, example.getMatcher().getMatchMode(), chain); return chain; }
@Override public <S extends T> Optional<S> findOne(Example<S> example) { try { return Optional.of( getQuery(new ExampleSpecification<S>(example), example.getProbeType(), Sort.unsorted()).getSingleResult()); } catch (NoResultException e) { return Optional.empty(); } }
switch (example.getMatcher().getDefaultStringMatcher()) { case EXACT: likeType = LikeType.EQUAL_TO; break; return ebeanServer.getExpressionFactory().exampleLike(example.getProbe(), example.getMatcher().isIgnoreCaseEnabled(), likeType);
private boolean isTypeRestricting(Example example) { if (example.getMatcher() instanceof UntypedExampleMatcher) { return false; } if (example.getMatcher().getIgnoredPaths().isEmpty()) { return true; } for (String path : example.getMatcher().getIgnoredPaths()) { if (this.converter.getTypeMapper().isTypeKey(path)) { return false; } } return true; }
@Override public <S extends T> Optional<S> findOne(Example<S> example) { Assert.notNull(example, "Sample must not be null!"); Query q = new Query(new Criteria().alike(example)); return Optional .ofNullable(mongoOperations.findOne(q, example.getProbeType(), entityInformation.getCollectionName())); }
/** * Returns the given {@link Example} as {@link Document} holding matching values extracted from * {@link Example#getProbe()}. * * @param example must not be {@literal null}. * @param entity must not be {@literal null}. * @return */ public Document getMappedExample(Example<?> example, MongoPersistentEntity<?> entity) { Assert.notNull(example, "Example must not be null!"); Assert.notNull(entity, "MongoPersistentEntity must not be null!"); Document reference = (Document) converter.convertToMongoType(example.getProbe()); if (entity.getIdProperty() != null && ClassUtils.isAssignable(entity.getType(), example.getProbeType())) { Object identifier = entity.getIdentifierAccessor(example.getProbe()).getIdentifier(); if (identifier == null) { reference.remove(entity.getIdProperty().getFieldName()); } } ExampleMatcherAccessor matcherAccessor = new ExampleMatcherAccessor(example.getMatcher()); applyPropertySpecs("", reference, example.getProbeType(), matcherAccessor); Document flattened = ObjectUtils.nullSafeEquals(NullHandler.INCLUDE, matcherAccessor.getNullHandler()) ? reference : new Document(SerializationUtils.flattenMap(reference)); Document result = example.getMatcher().isAllMatching() ? flattened : orConcatenate(flattened); return updateTypeRestrictions(result, example); }
/** * Retrieve a mapped {@link RedisOperationChain} to query secondary indexes given {@link Example}. * * @param example must not be {@literal null}. * @return the mapped {@link RedisOperationChain}. */ public RedisOperationChain getMappedExample(Example<?> example) { RedisOperationChain chain = new RedisOperationChain(); ExampleMatcherAccessor matcherAccessor = new ExampleMatcherAccessor(example.getMatcher()); applyPropertySpecs("", example.getProbe(), mappingContext.getRequiredPersistentEntity(example.getProbeType()), matcherAccessor, example.getMatcher().getMatchMode(), chain); return chain; }
/** * Static factory method to create a {@link Criteria} matching an example object. * * @param example must not be {@literal null}. * @return * @see Criteria#alike(Example) * @since 1.8 */ public static Criteria byExample(Object example) { return byExample(Example.of(example)); }