DATACASS-211 - Adapt to changes in Spring Data Commons.
Tweaked method signatures in CassandraRepositoryFactory after some signature changes in Spring Data Commons. Use newly introduced getTragetRepositoryViaReflection(…) to obtain the repository instance via the super class. Added repositoryBaseClass() attribute to @EnableCassandraRepositories. Related tickets: DATACMNS-542. Original pull request: #35.
This commit is contained in:
committed by
Oliver Gierke
parent
64eb1f738c
commit
71e24b2fdc
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013-2014 the original author or authors
|
||||
* Copyright 2013-2015 the original author or authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -27,6 +27,7 @@ import org.springframework.context.annotation.ComponentScan.Filter;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.data.cassandra.core.CassandraTemplate;
|
||||
import org.springframework.data.cassandra.repository.support.CassandraRepositoryFactoryBean;
|
||||
import org.springframework.data.repository.config.DefaultRepositoryBaseClass;
|
||||
import org.springframework.data.repository.query.QueryLookupStrategy;
|
||||
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
|
||||
|
||||
@@ -35,6 +36,7 @@ import org.springframework.data.repository.query.QueryLookupStrategy.Key;
|
||||
*
|
||||
* @author Alex Shvid
|
||||
* @author Matthew T. Adams
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@@ -107,6 +109,14 @@ public @interface EnableCassandraRepositories {
|
||||
*/
|
||||
Class<?> repositoryFactoryBeanClass() default CassandraRepositoryFactoryBean.class;
|
||||
|
||||
/**
|
||||
* Configure the repository base class to be used to create repository proxies for this particular configuration.
|
||||
*
|
||||
* @return
|
||||
* @since 1.3
|
||||
*/
|
||||
Class<?> repositoryBaseClass() default DefaultRepositoryBaseClass.class;
|
||||
|
||||
/**
|
||||
* Configures the name of the {@link CassandraTemplate} bean to be used with the repositories detected.
|
||||
*
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013-2014 the original author or authors
|
||||
* Copyright 2013-2015 the original author or authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -28,6 +28,7 @@ import org.springframework.data.cassandra.repository.query.CassandraQueryMethod;
|
||||
import org.springframework.data.cassandra.repository.query.StringBasedCassandraQuery;
|
||||
import org.springframework.data.mapping.model.MappingException;
|
||||
import org.springframework.data.repository.core.NamedQueries;
|
||||
import org.springframework.data.repository.core.RepositoryInformation;
|
||||
import org.springframework.data.repository.core.RepositoryMetadata;
|
||||
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
|
||||
import org.springframework.data.repository.query.QueryLookupStrategy;
|
||||
@@ -40,11 +41,12 @@ import org.springframework.util.Assert;
|
||||
*
|
||||
* @author Alex Shvid
|
||||
* @author Matthew T. Adams
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
|
||||
public class CassandraRepositoryFactory extends RepositoryFactorySupport {
|
||||
|
||||
private final CassandraOperations cassandraTemplate;
|
||||
private final CassandraOperations cassandraOperations;
|
||||
private final CassandraMappingContext mappingContext;
|
||||
|
||||
/**
|
||||
@@ -56,7 +58,7 @@ public class CassandraRepositoryFactory extends RepositoryFactorySupport {
|
||||
|
||||
Assert.notNull(cassandraOperations);
|
||||
|
||||
this.cassandraTemplate = cassandraOperations;
|
||||
this.cassandraOperations = cassandraOperations;
|
||||
this.mappingContext = cassandraOperations.getConverter().getMappingContext();
|
||||
|
||||
// TODO: remove when supporting declarative query methods
|
||||
@@ -69,13 +71,10 @@ public class CassandraRepositoryFactory extends RepositoryFactorySupport {
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
protected Object getTargetRepository(RepositoryMetadata metadata) {
|
||||
|
||||
CassandraEntityInformation<?, Serializable> entityInformation = getEntityInformation(metadata.getDomainType());
|
||||
|
||||
return new SimpleCassandraRepository(entityInformation, cassandraTemplate);
|
||||
protected Object getTargetRepository(RepositoryInformation information) {
|
||||
|
||||
CassandraEntityInformation<?, Serializable> entityInformation = getEntityInformation(information.getDomainType());
|
||||
return getTargetRepositoryViaReflection(information, entityInformation, cassandraOperations);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -90,7 +89,7 @@ public class CassandraRepositoryFactory extends RepositoryFactorySupport {
|
||||
}
|
||||
|
||||
return new MappingCassandraEntityInformation<T, ID>((CassandraPersistentEntity<T>) entity,
|
||||
cassandraTemplate.getConverter());
|
||||
cassandraOperations.getConverter());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -108,9 +107,9 @@ public class CassandraRepositoryFactory extends RepositoryFactorySupport {
|
||||
|
||||
if (namedQueries.hasQuery(namedQueryName)) {
|
||||
String namedQuery = namedQueries.getQuery(namedQueryName);
|
||||
return new StringBasedCassandraQuery(namedQuery, queryMethod, cassandraTemplate);
|
||||
return new StringBasedCassandraQuery(namedQuery, queryMethod, cassandraOperations);
|
||||
} else if (queryMethod.hasAnnotatedQuery()) {
|
||||
return new StringBasedCassandraQuery(queryMethod, cassandraTemplate);
|
||||
return new StringBasedCassandraQuery(queryMethod, cassandraOperations);
|
||||
} else {
|
||||
throw new InvalidDataAccessApiUsageException("declarative query methods are a todo");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright 2015 the original author or authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.cassandra.test.integration.repository;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.aop.framework.Advised;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.cassandra.core.CassandraOperations;
|
||||
import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories;
|
||||
import org.springframework.data.cassandra.repository.query.CassandraEntityInformation;
|
||||
import org.springframework.data.cassandra.repository.support.SimpleCassandraRepository;
|
||||
import org.springframework.data.cassandra.test.integration.support.IntegrationTestConfig;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
/**
|
||||
* Java config tests for {@link UserRepository}.
|
||||
*
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
@ContextConfiguration
|
||||
public class UserRepositoryJavaConfigCustomRepositoryBaseClassIntegrationTests extends
|
||||
UserRepositoryIntegrationTestsDelegator {
|
||||
|
||||
@Configuration
|
||||
@EnableCassandraRepositories(basePackageClasses = UserRepository.class,
|
||||
repositoryBaseClass = CustomCassandraRepository.class)
|
||||
public static class Config extends IntegrationTestConfig {
|
||||
|
||||
@Override
|
||||
public String[] getEntityBasePackages() {
|
||||
return new String[] { User.class.getPackage().getName() };
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACASS-211
|
||||
*/
|
||||
@Test
|
||||
public void targetRepositoryClassShouldBeConfiguredCustomBaseRepositoryClass() throws Exception {
|
||||
assertThat(((Advised) repository).getTargetSource().getTarget(), is(instanceOf(CustomCassandraRepository.class)));
|
||||
}
|
||||
|
||||
public static class CustomCassandraRepository<T, ID extends Serializable> extends SimpleCassandraRepository<T, ID> {
|
||||
|
||||
public CustomCassandraRepository(CassandraEntityInformation<T, ID> metadata, CassandraOperations operations) {
|
||||
super(metadata, operations);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user