Java Code Examples for org.springframework.data.repository.query.RepositoryQuery

The following examples show how to use org.springframework.data.repository.query.RepositoryQuery. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: sdn-rx   Source File: Neo4jQueryLookupStrategy.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata, ProjectionFactory factory,
	NamedQueries namedQueries) {

	Neo4jQueryMethod queryMethod = new Neo4jQueryMethod(method, metadata, factory);
	String namedQueryName = queryMethod.getNamedQueryName();

	if (namedQueries.hasQuery(namedQueryName)) {
		return StringBasedNeo4jQuery.create(neo4jOperations, mappingContext, evaluationContextProvider, queryMethod,
			namedQueries.getQuery(namedQueryName));
	} else if (queryMethod.hasQueryAnnotation()) {
		return StringBasedNeo4jQuery.create(neo4jOperations, mappingContext, evaluationContextProvider, queryMethod);
	} else {
		return PartTreeNeo4jQuery.create(neo4jOperations, mappingContext, queryMethod);
	}
}
 
Example 2
Source Project: sdn-rx   Source File: ReactiveNeo4jQueryLookupStrategy.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata, ProjectionFactory factory,
	NamedQueries namedQueries) {

	Neo4jQueryMethod queryMethod = new ReactiveNeo4jQueryMethod(method, metadata, factory);
	String namedQueryName = queryMethod.getNamedQueryName();

	if (namedQueries.hasQuery(namedQueryName)) {
		return ReactiveStringBasedNeo4jQuery
			.create(neo4jOperations, mappingContext, evaluationContextProvider, queryMethod,
			namedQueries.getQuery(namedQueryName));
	} else if (queryMethod.hasQueryAnnotation()) {
		return ReactiveStringBasedNeo4jQuery
			.create(neo4jOperations, mappingContext, evaluationContextProvider, queryMethod);
	} else {
		return ReactivePartTreeNeo4jQuery.create(neo4jOperations, mappingContext, queryMethod);
	}
}
 
Example 3
Source Project: sdn-rx   Source File: RepositoryQueryTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
void shouldSelectStringBasedNeo4jQueryForNamedQuery() {

	final String namedQueryName = "TestEntity.findAllByANamedQuery";
	when(namedQueries.hasQuery(namedQueryName)).thenReturn(true);
	when(namedQueries.getQuery(namedQueryName)).thenReturn("MATCH (n) RETURN n");

	final Neo4jQueryLookupStrategy lookupStrategy = new Neo4jQueryLookupStrategy(mock(Neo4jOperations.class),
		mock(
		Neo4jMappingContext.class), QueryMethodEvaluationContextProvider.DEFAULT);

	RepositoryQuery query = lookupStrategy
		.resolveQuery(queryMethod("findAllByANamedQuery"), TEST_REPOSITORY_METADATA,
			PROJECTION_FACTORY, namedQueries);
	assertThat(query).isInstanceOf(StringBasedNeo4jQuery.class);
}
 
Example 4
Source Project: spring-data   Source File: ArangoRepositoryFactory.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public RepositoryQuery resolveQuery(
	final Method method,
	final RepositoryMetadata metadata,
	final ProjectionFactory factory,
	final NamedQueries namedQueries) {

	final ArangoQueryMethod queryMethod = new ArangoQueryMethod(method, metadata, factory);
	final String namedQueryName = queryMethod.getNamedQueryName();

	if (namedQueries.hasQuery(namedQueryName)) {
		final String namedQuery = namedQueries.getQuery(namedQueryName);
		return new StringBasedArangoQuery(namedQuery, queryMethod, operations);
	} else if (queryMethod.hasAnnotatedQuery()) {
		return new StringBasedArangoQuery(queryMethod, operations);
	} else {
		return new DerivedArangoQuery(queryMethod, operations);
	}
}
 
Example 5
Source Project: dubbox   Source File: SolrRepositoryFactory.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata, NamedQueries namedQueries) {

	SolrQueryMethod queryMethod = new SolrQueryMethod(method, metadata, entityInformationCreator);
	String namedQueryName = queryMethod.getNamedQueryName();

	SolrOperations solrOperations = selectSolrOperations(metadata);

	if (namedQueries.hasQuery(namedQueryName)) {
		String namedQuery = namedQueries.getQuery(namedQueryName);
		return new StringBasedSolrQuery(namedQuery, queryMethod, solrOperations);
	} else if (queryMethod.hasAnnotatedQuery()) {
		return new StringBasedSolrQuery(queryMethod, solrOperations);
	} else {
		return new PartTreeSolrQuery(queryMethod, solrOperations);
	}
}
 
Example 6
@Override
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata,
		ProjectionFactory factory, NamedQueries namedQueries) {
	SpannerQueryMethod queryMethod = createQueryMethod(method, metadata, factory);
	Class<?> entityType = getEntityType(queryMethod);
	boolean isDml = queryMethod.getQueryAnnotation() != null && queryMethod.getQueryAnnotation().dmlStatement();

	if (queryMethod.hasAnnotatedQuery()) {
		Query query = queryMethod.getQueryAnnotation();
		return createSqlSpannerQuery(entityType, queryMethod, query.value(), isDml);
	}
	else if (namedQueries.hasQuery(queryMethod.getNamedQueryName())) {
		String sql = namedQueries.getQuery(queryMethod.getNamedQueryName());
		return createSqlSpannerQuery(entityType, queryMethod, sql, isDml);
	}

	return createPartTreeSpannerQuery(entityType, queryMethod);
}
 
Example 7
@Override
protected RepositoryQuery resolveQuery(EbeanQueryMethod method, EbeanServer ebeanServer, NamedQueries namedQueries) {
    RepositoryQuery query = EbeanQueryFactory.INSTANCE.fromQueryAnnotation(method, ebeanServer, evaluationContextProvider);

    if (null != query) {
        return query;
    }

    String name = method.getNamedQueryName();
    if (namedQueries.hasQuery(name)) {
        return EbeanQueryFactory.INSTANCE.fromMethodWithQueryString(method, ebeanServer, namedQueries.getQuery(name),
                evaluationContextProvider);
    }

    query = NamedEbeanQuery.lookupFrom(method, ebeanServer);

    if (null != query) {
        return query;
    }

    throw new IllegalStateException(
            String.format("Did neither find a NamedQuery nor an annotated query for method %s!", method));
}
 
Example 8
@Override
public void onCreation(RepositoryQuery query) {

	if (query instanceof PartTreeMybatisQuery) {
		new MybatisPartTreeMapperBuilder(configuration,
				mappingContext.getPersistentEntity(
						query.getQueryMethod().getEntityInformation().getJavaType()),
				(PartTreeMybatisQuery) query).build();
	}
	else if (query instanceof SimpleMybatisQuery) {
		new MybatisSimpleQueryMapperBuilder(configuration,
				mappingContext.getPersistentEntity(
						query.getQueryMethod().getEntityInformation().getJavaType()),
				(SimpleMybatisQuery) query).build();
	}
}
 
Example 9
Source Project: strategy-spring-security-acl   Source File: AclJpaQuery.java    License: Apache License 2.0 6 votes vote down vote up
private void installAclProxy(RepositoryQuery query) {
  CriteriaQuery<?> criteriaQuery = criteriaQuery();
  if (criteriaQuery == null) {
    logger.warn("Unable to install ACL Jpa Specification for method '" + method
            + "' and query: " + query + " ; query methods with Pageable/Sort are not (yet) supported");
    return;
  }
  this.cachedCriteriaQuery = criteriaQuery;
  this.root = root(cachedCriteriaQuery);

  try {
    this.aclPredicateTargetSource = installAclPredicateTargetSource();
  } catch (Exception e) {
    logger.warn(
        "Unable to install ACL Jpa Specification for method '" + method + "' and query: " + query + " : " +
        getStackTrace(e));
  }
}
 
Example 10
/**
 * @param key
 * @param evaluationContextProvider
 * @param keyValueOperations
 * @param queryCreator
 * @since 1.1
 */
public KeyValueQueryLookupStrategy(@Nullable Key key,
		QueryMethodEvaluationContextProvider evaluationContextProvider, KeyValueOperations keyValueOperations,
		Class<? extends AbstractQueryCreator<?, ?>> queryCreator,
		Class<? extends RepositoryQuery> repositoryQueryType) {

	Assert.notNull(evaluationContextProvider, "EvaluationContextProvider must not be null!");
	Assert.notNull(keyValueOperations, "KeyValueOperations must not be null!");
	Assert.notNull(queryCreator, "Query creator type must not be null!");
	Assert.notNull(repositoryQueryType, "RepositoryQueryType type must not be null!");

	this.evaluationContextProvider = evaluationContextProvider;
	this.keyValueOperations = keyValueOperations;
	this.queryCreator = queryCreator;
	this.repositoryQueryType = repositoryQueryType;
}
 
Example 11
@Override
@SuppressWarnings("unchecked")
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata, ProjectionFactory factory,
		NamedQueries namedQueries) {

	QueryMethod queryMethod = new QueryMethod(method, metadata, factory);

	Constructor<? extends KeyValuePartTreeQuery> constructor = (Constructor<? extends KeyValuePartTreeQuery>) ClassUtils
			.getConstructorIfAvailable(this.repositoryQueryType, QueryMethod.class,
					QueryMethodEvaluationContextProvider.class, KeyValueOperations.class, Class.class);

	Assert.state(constructor != null,
			String.format(
					"Constructor %s(QueryMethod, EvaluationContextProvider, KeyValueOperations, Class) not available!",
					ClassUtils.getShortName(this.repositoryQueryType)));

	return BeanUtils.instantiateClass(constructor, queryMethod, evaluationContextProvider, this.keyValueOperations,
			this.queryCreator);
}
 
Example 12
Source Project: sdn-rx   Source File: RepositoryQueryTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
void shouldSelectPartTreeNeo4jQuery() {

	final Neo4jQueryLookupStrategy lookupStrategy = new Neo4jQueryLookupStrategy(mock(Neo4jOperations.class),
		mock(
		Neo4jMappingContext.class), QueryMethodEvaluationContextProvider.DEFAULT);

	RepositoryQuery query = lookupStrategy
		.resolveQuery(queryMethod("findById", Object.class), TEST_REPOSITORY_METADATA, PROJECTION_FACTORY,
			namedQueries);
	assertThat(query).isInstanceOf(PartTreeNeo4jQuery.class);
}
 
Example 13
Source Project: sdn-rx   Source File: RepositoryQueryTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
void shouldSelectStringBasedNeo4jQuery() {

	final Neo4jQueryLookupStrategy lookupStrategy = new Neo4jQueryLookupStrategy(mock(Neo4jOperations.class),
		mock(
		Neo4jMappingContext.class), QueryMethodEvaluationContextProvider.DEFAULT);

	RepositoryQuery query = lookupStrategy
		.resolveQuery(queryMethod("annotatedQueryWithValidTemplate"), TEST_REPOSITORY_METADATA,
			PROJECTION_FACTORY, namedQueries);
	assertThat(query).isInstanceOf(StringBasedNeo4jQuery.class);
}
 
Example 14
Source Project: spring-data-cosmosdb   Source File: CosmosRepositoryFactory.java    License: MIT License 5 votes vote down vote up
@Override
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata,
                                    ProjectionFactory factory, NamedQueries namedQueries) {
    final CosmosQueryMethod queryMethod = new CosmosQueryMethod(method, metadata, factory);

    Assert.notNull(queryMethod, "queryMethod must not be null!");
    Assert.notNull(dbOperations, "dbOperations must not be null!");
    return new PartTreeCosmosQuery(queryMethod, dbOperations);

}
 
Example 15
@Override
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata,
                                    ProjectionFactory factory, NamedQueries namedQueries) {
    final ReactiveCosmosQueryMethod queryMethod = new ReactiveCosmosQueryMethod(method,
        metadata, factory);

    Assert.notNull(queryMethod, "queryMethod must not be null!");
    Assert.notNull(cosmosOperations, "dbOperations must not be null!");
    return new PartTreeReactiveCosmosQuery(queryMethod, cosmosOperations);

}
 
Example 16
@Override
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata repositoryMetadata,
		ProjectionFactory projectionFactory, NamedQueries namedQueries) {
	// In this method we usually decide if the query method is a PartTree or an annotated
	// @Query method.
	// There is no choice in Firestore. We only have PartTree.
	return new PartTreeFirestoreQuery(
			new FirestoreQueryMethod(method, repositoryMetadata, projectionFactory),
			this.firestoreTemplate,
			this.firestoreTemplate.getMappingContext(),
			this.firestoreTemplate.getClassMapper()
	);
}
 
Example 17
Source Project: spring-data-ebean   Source File: NamedEbeanQuery.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Looks up a named query for the given {@link org.springframework.data.repository.query.QueryMethod}.
 *
 * @param method
 * @return
 */
public static RepositoryQuery lookupFrom(EbeanQueryMethod method, EbeanServer ebeanServer) {
    final String queryName = method.getNamedQueryName();

    LOG.debug("Looking up named query {}", queryName);

    try {
        Query query = ebeanServer.createNamedQuery(method.getEntityInformation().getJavaType(), queryName);
        return new NamedEbeanQuery(method, ebeanServer, query);
    } catch (PersistenceException e) {
        LOG.debug("Did not find named query {}", queryName);
        return null;
    }
}
 
Example 18
@Override
protected RepositoryQuery resolveQuery(EbeanQueryMethod method, EbeanServer ebeanServer, NamedQueries namedQueries) {

    try {
        return new PartTreeEbeanQuery(method, ebeanServer);
    } catch (IllegalArgumentException e) {
        throw new IllegalArgumentException(
                String.format("Could not create query metamodel for method %s!", method.toString()), e);
    }
}
 
Example 19
@Override
protected RepositoryQuery resolveQuery(EbeanQueryMethod method, EbeanServer ebeanServer, NamedQueries namedQueries) {
    try {
        return lookupStrategy.resolveQuery(method, ebeanServer, namedQueries);
    } catch (IllegalStateException e) {
        return createStrategy.resolveQuery(method, ebeanServer, namedQueries);
    }
}
 
Example 20
@Override
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata, ProjectionFactory factory,
                                    NamedQueries namedQueries) {
    if (method.getAnnotation(TemplateQuery.class) == null) {
        return jpaQueryLookupStrategy.resolveQuery(method, metadata, factory, namedQueries);
    } else {
        return new FreemarkerTemplateQuery(new JpaQueryMethod(method, metadata, factory, extractor), entityManager);
    }
}
 
Example 21
Source Project: spring-vault   Source File: VaultRepositoryFactory.java    License: Apache License 2.0 5 votes vote down vote up
public VaultRepositoryFactory(KeyValueOperations keyValueOperations,
		Class<? extends AbstractQueryCreator<?, ?>> queryCreator,
		Class<? extends RepositoryQuery> repositoryQueryType) {
	super(keyValueOperations, queryCreator, repositoryQueryType);

	this.operations = keyValueOperations;
}
 
Example 22
Source Project: spring-vault   Source File: VaultRepositoryFactoryBean.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected VaultRepositoryFactory createRepositoryFactory(KeyValueOperations operations,
		Class<? extends AbstractQueryCreator<?, ?>> queryCreator,
		Class<? extends RepositoryQuery> repositoryQueryType) {

	return new VaultRepositoryFactory(operations, queryCreator, repositoryQueryType);
}
 
Example 23
@Override
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata repositoryMetadata,
                                    ProjectionFactory projectionFactory, NamedQueries namedQueries) {
  if (!isAggregateQueryAnnotated(method)) {
    return parentQueryLookupStrategy.resolveQuery(method, repositoryMetadata, projectionFactory, namedQueries);
  }
  else {
    return new AggregateMongoQuery(method, repositoryMetadata, mongoOperations, projectionFactory, queryExecutor);
  }
}
 
Example 24
@Test(dataProvider = "queryMethods", expectedExceptions = {JsonParseException.class})
public void testCreateAggregateQuery(String methodName) throws Exception {
  AggregateQuerySupportingRepositoryFactory factory = new AggregateQuerySupportingRepositoryFactory(mongoOperations,
                                                                                                    queryExecutor);
  QueryLookupStrategy lookupStrategy = factory.getQueryLookupStrategy(CREATE_IF_NOT_FOUND, evaluationContextProvider);

  Method method = TestAggregateRepository22.class.getMethod(methodName);
  RepositoryMetadata repositoryMetadata = new DefaultRepositoryMetadata(TestAggregateRepository22.class);
  ProjectionFactory projectionFactory = new SpelAwareProxyProjectionFactory();
  RepositoryQuery query = lookupStrategy.resolveQuery(method, repositoryMetadata, projectionFactory, null);
  assertNotNull(query);
  Object object = query.execute(new Object[0]);
  assertNull(object);
}
 
Example 25
@Override
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata repositoryMetadata,
                                    ProjectionFactory projectionFactory, NamedQueries namedQueries) {
  if (!isAggregateQueryAnnotated(method)) {
    return parentQueryLookupStrategy.resolveQuery(method, repositoryMetadata, projectionFactory, namedQueries);
  }
  else {
    return new NonReactiveAggregateMongoQuery(method, repositoryMetadata, mongoOperations, projectionFactory, queryExecutor);
  }
}
 
Example 26
@Override
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata repositoryMetadata,
                                    ProjectionFactory projectionFactory, NamedQueries namedQueries) {
  if (!isAggregateQueryAnnotated(method)) {
    return parentQueryLookupStrategy.resolveQuery(method, repositoryMetadata, projectionFactory, namedQueries);
  }
  else {
    return new ReactiveAggregateMongoQuery(method, repositoryMetadata, mongoOperations, projectionFactory, queryExecutor);
  }
}
 
Example 27
@Test(dataProvider = "queryMethods", expectedExceptions = {JsonParseException.class})
public void testCreateAggregateQuery(String methodName) throws Exception {
  ReactiveAggregateQuerySupportingRepositoryFactory factory = new ReactiveAggregateQuerySupportingRepositoryFactory(mongoOperations,
                                                                                                                    queryExecutor);
  Optional<QueryLookupStrategy> lookupStrategy = factory.getQueryLookupStrategy(CREATE_IF_NOT_FOUND, evaluationContextProvider);

  Assert.isTrue(lookupStrategy.isPresent(), "Lookup strategy must not be null");
  Method method = ReactiveTestAggregateRepository22.class.getMethod(methodName);
  RepositoryMetadata repositoryMetadata = new DefaultRepositoryMetadata(ReactiveTestAggregateRepository22.class);
  ProjectionFactory projectionFactory = new SpelAwareProxyProjectionFactory();
  RepositoryQuery query = lookupStrategy.get().resolveQuery(method, repositoryMetadata, projectionFactory, null);
  assertNotNull(query);
  Object object = query.execute(new Object[0]);
  assertNull(object);
}
 
Example 28
@Test(dataProvider = "queryMethods", expectedExceptions = {JsonParseException.class})
public void testCreateAggregateQuery(String methodName) throws Exception {
  NonReactiveAggregateQuerySupportingRepositoryFactory factory =
      new NonReactiveAggregateQuerySupportingRepositoryFactory(mongoOperations, queryExecutor);
  Optional<QueryLookupStrategy> lookupStrategy = factory.getQueryLookupStrategy(CREATE_IF_NOT_FOUND, evaluationContextProvider);
  assertTrue(lookupStrategy.isPresent(), "Expecting non null lookupStrategy");
  Method method = TestAggregateRepository22.class.getMethod(methodName);
  RepositoryMetadata repositoryMetadata = new DefaultRepositoryMetadata(TestAggregateRepository22.class);
  ProjectionFactory projectionFactory = new SpelAwareProxyProjectionFactory();
  RepositoryQuery query = lookupStrategy.get().resolveQuery(method, repositoryMetadata, projectionFactory, null);
  assertNotNull(query);
  Object object = query.execute(new Object[0]);
  assertNull(object);
}
 
Example 29
@Override
protected RepositoryQuery resolveQuery(MybatisQueryMethod method,
		SqlSessionTemplate sqlSessionTemplate, NamedQueries namedQueries) {

	if (method.isAnnotatedQuery()) {
		return new SimpleMybatisQuery(method, sqlSessionTemplate);
	}

	throw new IllegalStateException(String.format(
			"Did neither find a NamedQuery nor an annotated query for method %s!",
			method));
}
 
Example 30
@Override
protected RepositoryQuery resolveQuery(MybatisQueryMethod method,
		SqlSessionTemplate sqlSessionTemplate, NamedQueries namedQueries) {
	try {
		return lookupStrategy.resolveQuery(method, sqlSessionTemplate,
				namedQueries);
	}
	catch (IllegalStateException e) {
		return createStrategy.resolveQuery(method, sqlSessionTemplate,
				namedQueries);
	}
}