Java Code Examples for org.springframework.data.repository.core.NamedQueries

The following examples show how to use org.springframework.data.repository.core.NamedQueries. 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: 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 4
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 5
@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 6
@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 7
@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 8
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 9
@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 10
@Test
public void resolveSqlQueryTest() {
	String queryName = "fakeNamedQueryName";
	String query = "fake query";
	when(this.queryMethod.getNamedQueryName()).thenReturn(queryName);
	Query queryAnnotation = mock(Query.class);
	when(this.queryMethod.getQueryAnnotation()).thenReturn(queryAnnotation);
	NamedQueries namedQueries = mock(NamedQueries.class);

	Parameters parameters = mock(Parameters.class);

	Mockito.<Parameters>when(this.queryMethod.getParameters())
			.thenReturn(parameters);

	when(parameters.getNumberOfParameters()).thenReturn(1);
	when(parameters.getParameter(anyInt())).thenAnswer((invocation) -> {
		Parameter param = mock(Parameter.class);
		when(param.getName()).thenReturn(Optional.of("tag"));
		Mockito.<Class>when(param.getType()).thenReturn(Object.class);
		return param;
	});

	when(namedQueries.hasQuery(eq(queryName))).thenReturn(true);
	when(namedQueries.getQuery(eq(queryName))).thenReturn(query);

	this.datastoreQueryLookupStrategy.resolveQuery(null, null, null, namedQueries);

	verify(this.datastoreQueryLookupStrategy, times(1)).createGqlDatastoreQuery(
			eq(Object.class), same(this.queryMethod), eq(query));
}
 
Example 11
@Test
public void resolveSqlQueryTest() {
	String queryName = "fakeNamedQueryName";
	String query = "fake query";
	when(this.queryMethod.getNamedQueryName()).thenReturn(queryName);
	NamedQueries namedQueries = mock(NamedQueries.class);

	Parameters parameters = mock(Parameters.class);

	// @formatter:off
	Mockito.<Parameters>when(this.queryMethod.getParameters()).thenReturn(parameters);
	// @formatter:off

	when(parameters.getNumberOfParameters()).thenReturn(1);
	when(parameters.getParameter(anyInt())).thenAnswer((invocation) -> {
		Parameter param = mock(Parameter.class);
		when(param.getName()).thenReturn(Optional.of("tag"));
		// @formatter:off
		Mockito.<Class>when(param.getType()).thenReturn(Object.class);
		// @formatter:on
		return param;
	});

	when(namedQueries.hasQuery(eq(queryName))).thenReturn(true);
	when(namedQueries.getQuery(eq(queryName))).thenReturn(query);

	this.spannerQueryLookupStrategy.resolveQuery(null, null, null, namedQueries);

	verify(this.spannerQueryLookupStrategy, times(1)).createSqlSpannerQuery(
			eq(Object.class), same(this.queryMethod), eq(query), eq(false));
}
 
Example 12
@Test
public void resolvePartTreeQueryTest() {
	String queryName = "fakeNamedQueryName";
	when(this.queryMethod.getNamedQueryName()).thenReturn(queryName);
	NamedQueries namedQueries = mock(NamedQueries.class);
	when(namedQueries.hasQuery(any())).thenReturn(false);

	this.spannerQueryLookupStrategy.resolveQuery(null, null, null, namedQueries);

	verify(this.spannerQueryLookupStrategy, times(1))
			.createPartTreeSpannerQuery(eq(Object.class), same(this.queryMethod));
}
 
Example 13
@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 14
@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 15
@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 16
@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 17
@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 18
@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 19
@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 20
@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 21
@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);
	}
}
 
Example 22
/**
 * @since Spring data JPA 1.10.0
 */
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata,
      ProjectionFactory factory, NamedQueries namedQueries) {
  QueryLookupStrategy queryLookupStrategy =
      Factory.super.getQueryLookupStrategy(key, evaluationContextProvider);

  RepositoryQuery query = queryLookupStrategy.resolveQuery(method, metadata, factory, namedQueries);
  return wrapQuery(method, metadata, query);
}
 
Example 23
Source Project: genericdao   Source File: GenericQueryLookupStrategy.java    License: Artistic License 2.0 5 votes vote down vote up
@Override
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata, NamedQueries namedQueries) {
    if (method.getAnnotation(MybatisQuery.class) != null) {
        log.info(metadata.getRepositoryInterface().getName()+"."+method.getName()+" 为mybatis方法。 ");
        return new MybatisRepositoryQuery(sqlSessionTemplate , method , metadata) ;
    } else {
        return jpaQueryLookupStrategy.resolveQuery(method, metadata, namedQueries);
    }
}
 
Example 24
Source Project: ignite   Source File: IgniteRepositoryFactory.java    License: Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override protected QueryLookupStrategy getQueryLookupStrategy(final QueryLookupStrategy.Key key,
    EvaluationContextProvider evaluationCtxProvider) {

    return new QueryLookupStrategy() {
        @Override public RepositoryQuery resolveQuery(final Method mtd, final RepositoryMetadata metadata,
            final ProjectionFactory factory, NamedQueries namedQueries) {

            final Query annotation = mtd.getAnnotation(Query.class);

            if (annotation != null) {
                String qryStr = annotation.value();

                if (key != Key.CREATE && StringUtils.hasText(qryStr))
                    return new IgniteRepositoryQuery(metadata,
                        new IgniteQuery(qryStr, isFieldQuery(qryStr), IgniteQueryGenerator.getOptions(mtd)),
                        mtd, factory, ignite.getOrCreateCache(repoToCache.get(metadata.getRepositoryInterface())));
            }

            if (key == QueryLookupStrategy.Key.USE_DECLARED_QUERY)
                throw new IllegalStateException("To use QueryLookupStrategy.Key.USE_DECLARED_QUERY, pass " +
                    "a query string via org.apache.ignite.springdata.repository.config.Query annotation.");

            return new IgniteRepositoryQuery(metadata, IgniteQueryGenerator.generateSql(mtd, metadata), mtd,
                factory, ignite.getOrCreateCache(repoToCache.get(metadata.getRepositoryInterface())));
        }
    };
}
 
Example 25
/**
 * <p>
 * Use {@link HazelcastPartTreeQuery} for resolving queries against Hazelcast repositories.
 * </P>
 *
 * @param Method,             the query method
 * @param RepositoryMetadata, not used
 * @param ProjectionFactory,  not used
 * @param NamedQueries,       not used
 * @return A mechanism for querying Hazelcast repositories
 */
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata, ProjectionFactory projectionFactory,
                                    NamedQueries namedQueries) {

    HazelcastQueryMethod queryMethod = new HazelcastQueryMethod(method, metadata, projectionFactory);

    if (queryMethod.hasAnnotatedQuery()) {
        return new StringBasedHazelcastRepositoryQuery(queryMethod, hazelcastInstance);
    }

    return new HazelcastPartTreeQuery(queryMethod, evaluationContextProvider, this.keyValueOperations, this.queryCreator);
}
 
Example 26
@Override
protected <T, ID extends Serializable> RepositoryQuery createDynamoDBQuery(Method method, RepositoryMetadata metadata,
		Class<T> entityClass, Class<ID> idClass, NamedQueries namedQueries) {
	try {
		return new PartTreeDynamoDBQuery<T, ID>(dynamoDBOperations, new DynamoDBQueryMethod<T, ID>(method, metadata));
	} catch (IllegalArgumentException e) {
		throw new IllegalArgumentException(String.format("Could not create query metamodel for method %s!",
				method.toString()), e);
	}
}
 
Example 27
Source Project: spring-data-simpledb   Source File: SimpleDbQueryLookupStrategy.java    License: MIT License 5 votes vote down vote up
@Override
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata, NamedQueries namedQueries) {
	SimpleDbQueryMethod queryMethod;
	
	if(SimpleDbQueryMethod.isAnnotatedQuery(method)) {
		queryMethod = new SimpleDbQueryMethod(method, metadata, simpleDbOperations.getSimpleDb()
				.getSimpleDbDomain());
	} else {
		queryMethod = new SimpleDbPartTreeQueryMethod(method, metadata, simpleDbOperations.getSimpleDb()
				.getSimpleDbDomain());
	}
	
	return SimpleDbRepositoryQuery.fromQueryAnnotation(queryMethod, simpleDbOperations);
}
 
Example 28
@Override
public final RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata, ProjectionFactory factory,
                                          NamedQueries namedQueries) {
    return resolveQuery(new EbeanQueryMethod(method, metadata, factory), ebeanServer, namedQueries);
}
 
Example 29
@Override
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata,
		ProjectionFactory factory, NamedQueries namedQueries) {
	return resolveQuery(new MybatisQueryMethod(method, metadata, factory),
			sqlSessionTemplate, namedQueries);
}
 
Example 30
protected abstract RepositoryQuery resolveQuery(MybatisQueryMethod method,
SqlSessionTemplate sqlSessionTemplate, NamedQueries namedQueries);