Skip to content

DATAJDBC-234 - Support properties file based query declaration #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* parameters will get bound to the arguments of the annotated method.
*
* @author Jens Schauder
* @author Moises Cisneros
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
Expand All @@ -41,7 +42,13 @@
/**
* The SQL statement to execute when the annotated method gets invoked.
*/
String value();
String value() default "";

/**
* The named query to be used. If not defined, the name of
* {@code $ domainClass}.${queryMethodName}} will be used.
*/
String name() default "";

/**
* Optional {@link RowMapper} to use to convert the result of the query to domain class instances. Cannot be used
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
* @author Oliver Gierke
* @author Mark Paluch
* @author Maciej Walkowiak
* @author Moises Cisneros
*/
@RequiredArgsConstructor
class JdbcQueryLookupStrategy implements QueryLookupStrategy {
Expand All @@ -53,6 +54,7 @@ class JdbcQueryLookupStrategy implements QueryLookupStrategy {
private final JdbcConverter converter;
private final QueryMappingConfiguration queryMappingConfiguration;
private final NamedParameterJdbcOperations operations;


/*
* (non-Javadoc)
Expand All @@ -62,7 +64,7 @@ class JdbcQueryLookupStrategy implements QueryLookupStrategy {
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata repositoryMetadata,
ProjectionFactory projectionFactory, NamedQueries namedQueries) {

JdbcQueryMethod queryMethod = new JdbcQueryMethod(method, repositoryMetadata, projectionFactory);
JdbcQueryMethod queryMethod = new JdbcQueryMethod(method, repositoryMetadata, projectionFactory,namedQueries);

RowMapper<?> mapper = queryMethod.isModifyingQuery() ? null : createMapper(queryMethod);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,30 @@
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
import org.springframework.data.repository.core.NamedQueries;

/**
* {@link QueryMethod} implementation that implements a method by executing the query from a {@link Query} annotation on
* that method. Binds method arguments to named parameters in the SQL statement.
* {@link QueryMethod} implementation that implements a method by executing the
* query from a {@link Query} annotation on that method. Binds method arguments
* to named parameters in the SQL statement.
*
* @author Jens Schauder
* @author Kazuki Shimizu
* @author Moises Cisneros
* @deprecated Visibility of this class will be reduced to package private.
*/
@Deprecated
public class JdbcQueryMethod extends QueryMethod {

private final Method method;
private final NamedQueries namedQueries;

public JdbcQueryMethod(Method method, RepositoryMetadata metadata, ProjectionFactory factory) {
public JdbcQueryMethod(Method method, RepositoryMetadata metadata, ProjectionFactory factory,
NamedQueries namedQueries) {

super(method, metadata, factory);

this.namedQueries = namedQueries;
this.method = method;
}

Expand All @@ -53,13 +59,49 @@ public JdbcQueryMethod(Method method, RepositoryMetadata metadata, ProjectionFac
*
* @return May be {@code null}.
*/

@Nullable
String getAnnotatedQuery() {
public String getAnnotatedQuery() {
String annotatedValue = getQueryValue();
return StringUtils.hasText(annotatedValue) ? annotatedValue : getNamedQuery();
}

/**
* Returns the annotated query with key value if it exists.
*
* @return May be {@code null}.
*/
@Nullable
String getQueryValue() {
return getMergedAnnotationAttribute("value");
}


/**
* Returns the annotated query name.
*
* @return May be {@code null}.
*/
@Nullable
String getQueryName() {
return getMergedAnnotationAttribute("name");
}
/**
* Returns the class to be used as {@link org.springframework.jdbc.core.RowMapper}
* Returns the annotated query with key name if it exists.
*
* @return May be {@code null}.
*/
@Nullable
String getNamedQuery() {
String annotatedName = getMergedAnnotationAttribute("name");
return (StringUtils.hasText(annotatedName) && this.namedQueries.hasQuery(annotatedName))
? this.namedQueries.getQuery(annotatedName)
: this.namedQueries.getQuery(super.getName());
}

/*
* Returns the class to be used as {@link
* org.springframework.jdbc.core.RowMapper}
*
* @return May be {@code null}.
*/
Expand All @@ -69,7 +111,8 @@ Class<? extends RowMapper> getRowMapperClass() {
}

/**
* Returns the class to be used as {@link org.springframework.jdbc.core.ResultSetExtractor}
* Returns the class to be used as
* {@link org.springframework.jdbc.core.ResultSetExtractor}
*
* @return May be {@code null}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,32 @@
import org.junit.Rule;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.annotation.Id;
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactory;
import org.springframework.data.jdbc.testing.TestConfiguration;
import org.springframework.data.jdbc.testing.QueryNamedTestConfiguration;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.core.NamedQueries;
import org.springframework.data.repository.core.support.PropertiesBasedNamedQueries;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.rules.SpringClassRule;
import org.springframework.test.context.junit4.rules.SpringMethodRule;
import org.springframework.test.jdbc.JdbcTestUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.io.IOException;
import java.util.List;

import javax.annotation.PostConstruct;
;

/**
* Very simple use cases for creation and usage of JdbcRepositories.
Expand All @@ -49,13 +59,14 @@
@ContextConfiguration
@Transactional
public class JdbcRepositoryIntegrationTests {
public static final String DUMMY_SELECT_NAME = "DUMMY.SELECT";

@Configuration
@Import(TestConfiguration.class)
@Import(QueryNamedTestConfiguration.class)
static class Config {

@Autowired JdbcRepositoryFactory factory;

@Autowired
JdbcRepositoryFactory factory;
@Bean
Class<?> testClass() {
return JdbcRepositoryIntegrationTests.class;
Expand All @@ -64,15 +75,16 @@ Class<?> testClass() {
@Bean
DummyEntityRepository dummyEntityRepository() {
return factory.getRepository(DummyEntityRepository.class);
}

}
}

@ClassRule public static final SpringClassRule classRule = new SpringClassRule();
@Rule public SpringMethodRule methodRule = new SpringMethodRule();
@ClassRule
public static final SpringClassRule classRule = new SpringClassRule();
@Rule
public SpringMethodRule methodRule = new SpringMethodRule();

@Autowired NamedParameterJdbcTemplate template;
@Autowired DummyEntityRepository repository;
@Autowired NamedParameterJdbcTemplate template;
@Autowired DummyEntityRepository repository;

@Test // DATAJDBC-95
public void savesAnEntity() {
Expand Down Expand Up @@ -244,20 +256,30 @@ public void findByIdReturnsEmptyWhenNoneFound() {
assertThat(repository.findById(-1L)).isEmpty();
}

private static DummyEntity createDummyEntity() {
@Test // DATAJDBC-234
public void findAllQueryName() {

repository.save(createDummyEntity());
assertThat(repository.findAllQueryName().size() > 0);
}

private static DummyEntity createDummyEntity() {

DummyEntity entity = new DummyEntity();
entity.setName("Entity Name");
return entity;
}

interface DummyEntityRepository extends CrudRepository<DummyEntity, Long> {

@Query(name = DUMMY_SELECT_NAME)
List<DummyEntity> findAllQueryName();
}

@Data
static class DummyEntity {

String name;
@Id private Long idProp;
@Id
private Long idProp;
}
}
Loading