SimpleCassandraRepository refactoring
This commit is contained in:
@@ -28,7 +28,7 @@ import com.datastax.driver.core.TableMetadata;
|
||||
*/
|
||||
public class CassandraAdminTemplate implements CassandraAdminOperations {
|
||||
|
||||
private static Logger log = LoggerFactory.getLogger(CassandraAdminTemplate.class);
|
||||
private static final Logger log = LoggerFactory.getLogger(CassandraAdminTemplate.class);
|
||||
|
||||
private SpringDataKeyspace keyspace;
|
||||
private Session session;
|
||||
|
||||
@@ -1204,6 +1204,7 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr
|
||||
try {
|
||||
|
||||
final Query q = CqlUtils.toInsertQuery(keyspace, tableName, entity, optionsByName, cassandraConverter);
|
||||
|
||||
logger.info(q.toString());
|
||||
if (q.getConsistencyLevel() != null) {
|
||||
logger.info(q.getConsistencyLevel().name());
|
||||
|
||||
@@ -17,8 +17,7 @@ package org.springframework.data.cassandra.repository.support;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.springframework.cassandra.core.CassandraOperations;
|
||||
import org.springframework.data.cassandra.core.CassandraDataOperations;
|
||||
import org.springframework.data.cassandra.core.CassandraDataTemplate;
|
||||
import org.springframework.data.cassandra.mapping.CassandraPersistentEntity;
|
||||
import org.springframework.data.cassandra.mapping.CassandraPersistentProperty;
|
||||
import org.springframework.data.cassandra.repository.CassandraRepository;
|
||||
@@ -38,8 +37,7 @@ import org.springframework.util.Assert;
|
||||
|
||||
public class CassandraRepositoryFactory extends RepositoryFactorySupport {
|
||||
|
||||
private final CassandraOperations operations;
|
||||
private final CassandraDataOperations dataOperations;
|
||||
private final CassandraDataTemplate cassandraDataTemplate;
|
||||
private final MappingContext<? extends CassandraPersistentEntity<?>, CassandraPersistentProperty> mappingContext;
|
||||
|
||||
/**
|
||||
@@ -47,14 +45,12 @@ public class CassandraRepositoryFactory extends RepositoryFactorySupport {
|
||||
*
|
||||
* @param mongoOperations must not be {@literal null}
|
||||
*/
|
||||
public CassandraRepositoryFactory(CassandraOperations operations, CassandraDataOperations dataOperations) {
|
||||
public CassandraRepositoryFactory(CassandraDataTemplate cassandraDataTemplate) {
|
||||
|
||||
Assert.notNull(operations);
|
||||
Assert.notNull(dataOperations);
|
||||
Assert.notNull(cassandraDataTemplate);
|
||||
|
||||
this.operations = operations;
|
||||
this.dataOperations = dataOperations;
|
||||
this.mappingContext = dataOperations.getConverter().getMappingContext();
|
||||
this.cassandraDataTemplate = cassandraDataTemplate;
|
||||
this.mappingContext = cassandraDataTemplate.getConverter().getMappingContext();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -68,7 +64,7 @@ public class CassandraRepositoryFactory extends RepositoryFactorySupport {
|
||||
|
||||
CassandraEntityInformation<?, Serializable> entityInformation = getEntityInformation(metadata.getDomainType());
|
||||
|
||||
return new SimpleCassandraRepository(entityInformation, operations, dataOperations);
|
||||
return new SimpleCassandraRepository(entityInformation, cassandraDataTemplate);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -17,8 +17,7 @@ package org.springframework.data.cassandra.repository.support;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.springframework.cassandra.core.CassandraOperations;
|
||||
import org.springframework.data.cassandra.core.CassandraDataOperations;
|
||||
import org.springframework.data.cassandra.core.CassandraDataTemplate;
|
||||
import org.springframework.data.cassandra.repository.CassandraRepository;
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport;
|
||||
@@ -34,31 +33,21 @@ import org.springframework.util.Assert;
|
||||
public class CassandraRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable> extends
|
||||
RepositoryFactoryBeanSupport<T, S, ID> {
|
||||
|
||||
private CassandraOperations operations;
|
||||
private CassandraDataOperations dataOperations;
|
||||
private CassandraDataTemplate cassandraDataTemplate;
|
||||
|
||||
@Override
|
||||
protected RepositoryFactorySupport createRepositoryFactory() {
|
||||
return new CassandraRepositoryFactory(operations, dataOperations);
|
||||
return new CassandraRepositoryFactory(cassandraDataTemplate);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the {@link CassandraOperations} to be used.
|
||||
* Configures the {@link CassandraDataTemplate} to be used.
|
||||
*
|
||||
* @param operations the operations to set
|
||||
*/
|
||||
public void setCassandraOperations(CassandraOperations operations) {
|
||||
this.operations = operations;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the {@link CassandraDataOperations} to be used.
|
||||
*
|
||||
* @param operations the operations to set
|
||||
*/
|
||||
public void setCassandraDataOperations(CassandraDataOperations dataOperations) {
|
||||
this.dataOperations = dataOperations;
|
||||
setMappingContext(dataOperations.getConverter().getMappingContext());
|
||||
public void setCassandraDataTemplate(CassandraDataTemplate cassandraDataTemplate) {
|
||||
this.cassandraDataTemplate = cassandraDataTemplate;
|
||||
setMappingContext(cassandraDataTemplate.getConverter().getMappingContext());
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -71,7 +60,7 @@ public class CassandraRepositoryFactoryBean<T extends Repository<S, ID>, S, ID e
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
super.afterPropertiesSet();
|
||||
Assert.notNull(dataOperations, "CassandraDataOperations must not be null!");
|
||||
Assert.notNull(cassandraDataTemplate, "cassandraDataTemplate must not be null!");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ import com.datastax.driver.core.querybuilder.Clause;
|
||||
import com.datastax.driver.core.querybuilder.Delete;
|
||||
import com.datastax.driver.core.querybuilder.QueryBuilder;
|
||||
import com.datastax.driver.core.querybuilder.Select;
|
||||
import com.datastax.driver.core.querybuilder.Truncate;
|
||||
|
||||
/**
|
||||
* Repository base implementation for Cassandra.
|
||||
@@ -41,8 +42,7 @@ import com.datastax.driver.core.querybuilder.Select;
|
||||
|
||||
public class SimpleCassandraRepository<T, ID extends Serializable> implements CassandraRepository<T, ID> {
|
||||
|
||||
private final CassandraOperations operations;
|
||||
private final CassandraDataOperations dataOperations;
|
||||
private final CassandraDataTemplate cassandraDataTemplate;
|
||||
private final CassandraEntityInformation<T, ID> entityInformation;
|
||||
|
||||
/**
|
||||
@@ -52,16 +52,14 @@ public class SimpleCassandraRepository<T, ID extends Serializable> implements Ca
|
||||
* @param metadata must not be {@literal null}.
|
||||
* @param template must not be {@literal null}.
|
||||
*/
|
||||
public SimpleCassandraRepository(CassandraEntityInformation<T, ID> metadata, CassandraOperations operations,
|
||||
CassandraDataOperations dataOperations) {
|
||||
public SimpleCassandraRepository(CassandraEntityInformation<T, ID> metadata,
|
||||
CassandraDataTemplate cassandraDataTemplate) {
|
||||
|
||||
Assert.notNull(operations);
|
||||
Assert.notNull(dataOperations);
|
||||
Assert.notNull(cassandraDataTemplate);
|
||||
Assert.notNull(metadata);
|
||||
|
||||
this.entityInformation = metadata;
|
||||
this.operations = operations;
|
||||
this.dataOperations = dataOperations;
|
||||
this.cassandraDataTemplate = cassandraDataTemplate;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -71,10 +69,7 @@ public class SimpleCassandraRepository<T, ID extends Serializable> implements Ca
|
||||
public <S extends T> S save(S entity) {
|
||||
|
||||
Assert.notNull(entity, "Entity must not be null!");
|
||||
|
||||
// INSERT OR OPDATE?
|
||||
|
||||
dataOperations.update(entity, entityInformation.getTableName());
|
||||
cassandraDataTemplate.insert(entity, entityInformation.getTableName());
|
||||
return entity;
|
||||
}
|
||||
|
||||
@@ -111,7 +106,7 @@ public class SimpleCassandraRepository<T, ID extends Serializable> implements Ca
|
||||
Select select = QueryBuilder.select().all().from(entityInformation.getTableName());
|
||||
select.where(getIdClause(id));
|
||||
|
||||
return dataOperations.selectOne(select, entityInformation.getJavaType());
|
||||
return cassandraDataTemplate.selectOne(select, entityInformation.getJavaType());
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -125,7 +120,7 @@ public class SimpleCassandraRepository<T, ID extends Serializable> implements Ca
|
||||
Select select = QueryBuilder.select().countAll().from(entityInformation.getTableName());
|
||||
select.where(getIdClause(id));
|
||||
|
||||
Long num = dataOperations.count(select);
|
||||
Long num = cassandraDataTemplate.count(select);
|
||||
return num != null && num.longValue() > 0;
|
||||
}
|
||||
|
||||
@@ -134,7 +129,7 @@ public class SimpleCassandraRepository<T, ID extends Serializable> implements Ca
|
||||
* @see org.springframework.data.repository.CrudRepository#count()
|
||||
*/
|
||||
public long count() {
|
||||
return dataOperations.count(entityInformation.getTableName());
|
||||
return cassandraDataTemplate.count(entityInformation.getTableName());
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -147,7 +142,7 @@ public class SimpleCassandraRepository<T, ID extends Serializable> implements Ca
|
||||
Delete delete = QueryBuilder.delete().all().from(entityInformation.getTableName());
|
||||
delete.where(getIdClause(id));
|
||||
|
||||
operations.execute(delete.getQueryString());
|
||||
cassandraDataTemplate.execute(delete.getQueryString());
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -177,8 +172,7 @@ public class SimpleCassandraRepository<T, ID extends Serializable> implements Ca
|
||||
* @see org.springframework.data.repository.CrudRepository#deleteAll()
|
||||
*/
|
||||
public void deleteAll() {
|
||||
Delete delete = QueryBuilder.delete().all().from(entityInformation.getTableName());
|
||||
operations.execute(delete.getQueryString());
|
||||
// cassandraDataTemplate.truncate(entityInformation.getTableName());
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -213,7 +207,7 @@ public class SimpleCassandraRepository<T, ID extends Serializable> implements Ca
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
return dataOperations.select(query, entityInformation.getJavaType());
|
||||
return cassandraDataTemplate.select(query, entityInformation.getJavaType());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -222,7 +216,7 @@ public class SimpleCassandraRepository<T, ID extends Serializable> implements Ca
|
||||
* @return
|
||||
*/
|
||||
protected CassandraOperations getCassandraOperations() {
|
||||
return this.operations;
|
||||
return this.cassandraDataTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -231,7 +225,7 @@ public class SimpleCassandraRepository<T, ID extends Serializable> implements Ca
|
||||
* @return
|
||||
*/
|
||||
protected CassandraDataOperations getCassandraDataOperations() {
|
||||
return this.dataOperations;
|
||||
return this.cassandraDataTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
/*
|
||||
* Copyright 2011 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;
|
||||
|
||||
/**
|
||||
* Sample domain class got from Webby social network site.
|
||||
*
|
||||
* @author Alex Shvid
|
||||
*
|
||||
*/
|
||||
public class Profile {
|
||||
|
||||
public enum Gender {
|
||||
MALE, FEMALE;
|
||||
}
|
||||
|
||||
private Long profileId;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private Gender gender;
|
||||
|
||||
public Long getProfileId() {
|
||||
return profileId;
|
||||
}
|
||||
|
||||
public void setProfileId(Long profileId) {
|
||||
this.profileId = profileId;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public Gender getGender() {
|
||||
return gender;
|
||||
}
|
||||
|
||||
public void setGender(Gender gender) {
|
||||
this.gender = gender;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,13 +16,14 @@
|
||||
package org.springframework.data.cassandra.test.integration.repository;
|
||||
|
||||
import org.springframework.data.cassandra.repository.CassandraRepository;
|
||||
import org.springframework.data.cassandra.test.integration.table.User;
|
||||
|
||||
/**
|
||||
* Sample repository managing {@link Profile} entities.
|
||||
* Sample repository managing {@link User} entities.
|
||||
*
|
||||
* @author Alex Shvid
|
||||
*
|
||||
*/
|
||||
public interface ProfileRepository extends CassandraRepository<Profile, Long> {
|
||||
public interface UserRepository extends CassandraRepository<User, String> {
|
||||
|
||||
}
|
||||
@@ -28,24 +28,27 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.cassandra.core.CassandraDataOperations;
|
||||
import org.springframework.data.cassandra.test.integration.table.User;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* Base class for tests for {@link ProfileRepository}.
|
||||
* Base class for tests for {@link UserRepository}.
|
||||
*
|
||||
* @author Alex Shvid
|
||||
*
|
||||
*/
|
||||
@ContextConfiguration
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class ProfileRepositoryIntegrationTests {
|
||||
|
||||
// @Autowired
|
||||
// protected ProfileRepository repository;
|
||||
public class UserRepositoryIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
CassandraDataOperations operations;
|
||||
protected UserRepository repository;
|
||||
|
||||
@Autowired
|
||||
protected CassandraDataOperations dataOperations;
|
||||
|
||||
User alex;
|
||||
|
||||
@BeforeClass
|
||||
public static void startCassandra() throws IOException, TTransportException, ConfigurationException,
|
||||
@@ -56,7 +59,16 @@ public class ProfileRepositoryIntegrationTests {
|
||||
@Before
|
||||
public void setUp() throws InterruptedException {
|
||||
|
||||
// repository.deleteAll();
|
||||
repository.deleteAll();
|
||||
|
||||
alex = new User();
|
||||
alex.setUsername("alex");
|
||||
alex.setFirstName("Alex");
|
||||
alex.setLastName("Shvid");
|
||||
alex.setPassword("123");
|
||||
alex.setPlace("SF");
|
||||
|
||||
dataOperations.insert(alex);
|
||||
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ import org.springframework.data.cassandra.mapping.Index;
|
||||
import org.springframework.data.cassandra.mapping.Table;
|
||||
|
||||
/**
|
||||
* This is an example of the Users statis table, where all fields are columns in Cassandra row. Some fields can be
|
||||
* This is an example of the Users status table, where all fields are columns in Cassandra row. Some fields can be
|
||||
* Set,List,Map like emails.
|
||||
*
|
||||
* User contains base information related for separate user, like names, additional information, emails, following
|
||||
@@ -63,9 +63,9 @@ public class User {
|
||||
private String password;
|
||||
|
||||
/*
|
||||
* Age
|
||||
* Birth Year
|
||||
*/
|
||||
private int age;
|
||||
private int birthYear;
|
||||
|
||||
/*
|
||||
* Following other users in userline
|
||||
@@ -142,17 +142,17 @@ public class User {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Returns the age.
|
||||
* @return Returns the birthYear.
|
||||
*/
|
||||
public int getAge() {
|
||||
return age;
|
||||
public int getBirthYear() {
|
||||
return birthYear;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param age The age to set.
|
||||
* @param birthYear The birthYear to set.
|
||||
*/
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
public void setBirthYear(int birthYear) {
|
||||
this.birthYear = birthYear;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1 +1 @@
|
||||
Profile.findByNamedQuery=SELECT firstName FROM table WHERE firstName=?0
|
||||
User.findByNamedQuery=SELECT firstName FROM table WHERE firstName=?0
|
||||
|
||||
@@ -61,9 +61,8 @@
|
||||
</bean>
|
||||
|
||||
<bean class="org.springframework.data.cassandra.repository.support.CassandraRepositoryFactoryBean">
|
||||
<property name="cassandraOperations" ref="cassandraTemplate"/>
|
||||
<property name="cassandraDataOperations" ref="cassandraDataTemplate"/>
|
||||
<property name="repositoryInterface" value="org.springframework.data.cassandra.test.integration.repository.ProfileRepository"/>
|
||||
<property name="cassandraDataTemplate" ref="cassandraDataTemplate"/>
|
||||
<property name="repositoryInterface" value="org.springframework.data.cassandra.test.integration.repository.UserRepository"/>
|
||||
<property name="namedQueries">
|
||||
<bean class="org.springframework.data.repository.core.support.PropertiesBasedNamedQueries">
|
||||
<constructor-arg>
|
||||
Reference in New Issue
Block a user