created SimpleCassandraRepository
This commit is contained in:
@@ -50,6 +50,16 @@ public interface CassandraDataOperations {
|
||||
*/
|
||||
<T> List<T> select(String cql, Class<T> selectClass);
|
||||
|
||||
/**
|
||||
* Execute query and convert ResultSet to the list of entities
|
||||
*
|
||||
* @param selectQuery must not be {@literal null}.
|
||||
* @param selectClass must not be {@literal null}, mapped entity type.
|
||||
* @return
|
||||
*/
|
||||
|
||||
<T> List<T> select(Select selectQuery, Class<T> selectClass);
|
||||
|
||||
/**
|
||||
* Execute query and convert ResultSet to the entity
|
||||
*
|
||||
@@ -59,12 +69,26 @@ public interface CassandraDataOperations {
|
||||
*/
|
||||
<T> T selectOne(String cql, Class<T> selectClass);
|
||||
|
||||
<T> List<T> select(Select selectQuery, Class<T> selectClass);
|
||||
|
||||
<T> T selectOne(Select selectQuery, Class<T> selectClass);
|
||||
|
||||
/**
|
||||
* Counts rows for given query
|
||||
*
|
||||
* @param selectQuery
|
||||
* @return
|
||||
*/
|
||||
|
||||
Long count(Select selectQuery);
|
||||
|
||||
/**
|
||||
* Counts all rows for given table
|
||||
*
|
||||
* @param tableName
|
||||
* @return
|
||||
*/
|
||||
|
||||
Long count(String tableName);
|
||||
|
||||
/**
|
||||
* Insert the given object to the table by id.
|
||||
*
|
||||
|
||||
@@ -18,7 +18,6 @@ package org.springframework.data.cassandra.core;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
@@ -43,6 +42,7 @@ import com.datastax.driver.core.ResultSet;
|
||||
import com.datastax.driver.core.Row;
|
||||
import com.datastax.driver.core.Session;
|
||||
import com.datastax.driver.core.querybuilder.Batch;
|
||||
import com.datastax.driver.core.querybuilder.QueryBuilder;
|
||||
import com.datastax.driver.core.querybuilder.Select;
|
||||
|
||||
/**
|
||||
@@ -123,13 +123,22 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.cassandra.core.CassandraOperations#selectCount(com.datastax.driver.core.querybuilder.Select)
|
||||
* @see org.springframework.data.cassandra.core.CassandraOperations#count(com.datastax.driver.core.querybuilder.Select)
|
||||
*/
|
||||
@Override
|
||||
public Long count(Select selectQuery) {
|
||||
return doSelectCount(selectQuery);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.cassandra.core.CassandraOperations#count(java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public Long count(String tableName) {
|
||||
Select select = QueryBuilder.select().countAll().from(tableName);
|
||||
return doSelectCount(select);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.cassandra.core.CassandraOperations#delete(java.util.List)
|
||||
*/
|
||||
@@ -165,7 +174,8 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr
|
||||
*/
|
||||
@Override
|
||||
public <T> void delete(List<T> entities, String tableName) {
|
||||
delete(entities, tableName, new HashMap<String, Object>());
|
||||
Map<String, Object> defaultOptions = Collections.emptyMap();
|
||||
delete(entities, tableName, defaultOptions);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -223,7 +233,8 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr
|
||||
*/
|
||||
@Override
|
||||
public <T> void delete(T entity, String tableName) {
|
||||
delete(entity, tableName, new HashMap<String, Object>());
|
||||
Map<String, Object> defaultOptions = Collections.emptyMap();
|
||||
delete(entity, tableName, defaultOptions);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -280,7 +291,8 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr
|
||||
*/
|
||||
@Override
|
||||
public <T> void deleteAsynchronously(List<T> entities, String tableName) {
|
||||
insertAsynchronously(entities, tableName, new HashMap<String, Object>());
|
||||
Map<String, Object> defaultOptions = Collections.emptyMap();
|
||||
insertAsynchronously(entities, tableName, defaultOptions);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -338,7 +350,8 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr
|
||||
*/
|
||||
@Override
|
||||
public <T> void deleteAsynchronously(T entity, String tableName) {
|
||||
deleteAsynchronously(entity, tableName, new HashMap<String, Object>());
|
||||
Map<String, Object> defaultOptions = Collections.emptyMap();
|
||||
deleteAsynchronously(entity, tableName, defaultOptions);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -430,7 +443,8 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr
|
||||
*/
|
||||
@Override
|
||||
public <T> List<T> insert(List<T> entities, String tableName) {
|
||||
return insert(entities, tableName, new HashMap<String, Object>());
|
||||
Map<String, Object> defaultOptions = Collections.emptyMap();
|
||||
return insert(entities, tableName, defaultOptions);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -488,7 +502,8 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr
|
||||
*/
|
||||
@Override
|
||||
public <T> T insert(T entity, String tableName) {
|
||||
return insert(entity, tableName, new HashMap<String, Object>());
|
||||
Map<String, Object> defaultOptions = Collections.emptyMap();
|
||||
return insert(entity, tableName, defaultOptions);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -545,7 +560,8 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr
|
||||
*/
|
||||
@Override
|
||||
public <T> List<T> insertAsynchronously(List<T> entities, String tableName) {
|
||||
return insertAsynchronously(entities, tableName, new HashMap<String, Object>());
|
||||
Map<String, Object> defaultOptions = Collections.emptyMap();
|
||||
return insertAsynchronously(entities, tableName, defaultOptions);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -603,7 +619,8 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr
|
||||
*/
|
||||
@Override
|
||||
public <T> T insertAsynchronously(T entity, String tableName) {
|
||||
return insertAsynchronously(entity, tableName, new HashMap<String, Object>());
|
||||
Map<String, Object> defaultOptions = Collections.emptyMap();
|
||||
return insertAsynchronously(entity, tableName, defaultOptions);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -695,7 +712,8 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr
|
||||
*/
|
||||
@Override
|
||||
public <T> List<T> update(List<T> entities, String tableName) {
|
||||
return update(entities, tableName, new HashMap<String, Object>());
|
||||
Map<String, Object> defaultOptions = Collections.emptyMap();
|
||||
return update(entities, tableName, defaultOptions);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -753,7 +771,8 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr
|
||||
*/
|
||||
@Override
|
||||
public <T> T update(T entity, String tableName) {
|
||||
return update(entity, tableName, new HashMap<String, Object>());
|
||||
Map<String, Object> defaultOptions = Collections.emptyMap();
|
||||
return update(entity, tableName, defaultOptions);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -810,7 +829,8 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr
|
||||
*/
|
||||
@Override
|
||||
public <T> List<T> updateAsynchronously(List<T> entities, String tableName) {
|
||||
return updateAsynchronously(entities, tableName, new HashMap<String, Object>());
|
||||
Map<String, Object> defaultOptions = Collections.emptyMap();
|
||||
return updateAsynchronously(entities, tableName, defaultOptions);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -868,7 +888,8 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr
|
||||
*/
|
||||
@Override
|
||||
public <T> T updateAsynchronously(T entity, String tableName) {
|
||||
return updateAsynchronously(entity, tableName, new HashMap<String, Object>());
|
||||
Map<String, Object> defaultOptions = Collections.emptyMap();
|
||||
return updateAsynchronously(entity, tableName, defaultOptions);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* 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.repository.query;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.springframework.data.repository.core.EntityInformation;
|
||||
|
||||
/**
|
||||
* Cassandra specific {@link EntityInformation}.
|
||||
*
|
||||
* @author Alex Shvid
|
||||
*
|
||||
*/
|
||||
public interface CassandraEntityInformation<T, ID extends Serializable> extends EntityInformation<T, ID> {
|
||||
|
||||
/**
|
||||
* Returns the name of the table the entity shall be persisted to.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
String getTableName();
|
||||
|
||||
/**
|
||||
* Returns the column that the id will be persisted to.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
String getIdColumn();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* Copyright 2010-2012 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.repository.support;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.springframework.cassandra.core.CassandraOperations;
|
||||
import org.springframework.data.cassandra.core.CassandraDataOperations;
|
||||
import org.springframework.data.cassandra.mapping.CassandraPersistentEntity;
|
||||
import org.springframework.data.cassandra.mapping.CassandraPersistentProperty;
|
||||
import org.springframework.data.cassandra.repository.CassandraRepository;
|
||||
import org.springframework.data.cassandra.repository.query.CassandraEntityInformation;
|
||||
import org.springframework.data.mapping.context.MappingContext;
|
||||
import org.springframework.data.mapping.model.MappingException;
|
||||
import org.springframework.data.repository.core.RepositoryMetadata;
|
||||
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Factory to create {@link CassandraRepository} instances.
|
||||
*
|
||||
* @author Alex Shvid
|
||||
*
|
||||
*/
|
||||
|
||||
public class CassandraRepositoryFactory extends RepositoryFactorySupport {
|
||||
|
||||
private final CassandraOperations operations;
|
||||
private final CassandraDataOperations dataOperations;
|
||||
private final MappingContext<? extends CassandraPersistentEntity<?>, CassandraPersistentProperty> mappingContext;
|
||||
|
||||
/**
|
||||
* Creates a new {@link MongoRepositoryFactory} with the given {@link MongoOperations}.
|
||||
*
|
||||
* @param mongoOperations must not be {@literal null}
|
||||
*/
|
||||
public CassandraRepositoryFactory(CassandraOperations operations, CassandraDataOperations dataOperations) {
|
||||
|
||||
Assert.notNull(operations);
|
||||
Assert.notNull(dataOperations);
|
||||
|
||||
this.operations = operations;
|
||||
this.dataOperations = dataOperations;
|
||||
this.mappingContext = dataOperations.getConverter().getMappingContext();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
|
||||
return SimpleCassandraRepository.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
protected Object getTargetRepository(RepositoryMetadata metadata) {
|
||||
|
||||
CassandraEntityInformation<?, Serializable> entityInformation = getEntityInformation(metadata.getDomainType());
|
||||
|
||||
return new SimpleCassandraRepository(entityInformation, operations, dataOperations);
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.core.support.RepositoryFactorySupport#getEntityInformation(java.lang.Class)
|
||||
*/
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T, ID extends Serializable> CassandraEntityInformation<T, ID> getEntityInformation(Class<T> domainClass) {
|
||||
|
||||
CassandraPersistentEntity<?> entity = mappingContext.getPersistentEntity(domainClass);
|
||||
|
||||
if (entity == null) {
|
||||
throw new MappingException(String.format("Could not lookup mapping metadata for domain class %s!",
|
||||
domainClass.getName()));
|
||||
}
|
||||
|
||||
return new MappingCassandraEntityInformation<T, ID>((CassandraPersistentEntity<T>) entity);
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,15 @@
|
||||
*/
|
||||
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.repository.CassandraRepository;
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport;
|
||||
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* {@link org.springframework.beans.factory.FactoryBean} to create {@link CassandraRepository} instances.
|
||||
@@ -23,6 +31,47 @@ import org.springframework.data.cassandra.repository.CassandraRepository;
|
||||
* @author Alex Shvid
|
||||
*
|
||||
*/
|
||||
public class CassandraRepositoryFactoryBean {
|
||||
public class CassandraRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable> extends
|
||||
RepositoryFactoryBeanSupport<T, S, ID> {
|
||||
|
||||
private CassandraOperations operations;
|
||||
private CassandraDataOperations dataOperations;
|
||||
|
||||
@Override
|
||||
protected RepositoryFactorySupport createRepositoryFactory() {
|
||||
return new CassandraRepositoryFactory(operations, dataOperations);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the {@link CassandraOperations} 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());
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see
|
||||
* org.springframework.data.repository.support.RepositoryFactoryBeanSupport
|
||||
* #afterPropertiesSet()
|
||||
*/
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
super.afterPropertiesSet();
|
||||
Assert.notNull(dataOperations, "CassandraDataOperations must not be null!");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* Copyright (c) 2011 by the original author(s).
|
||||
*
|
||||
* 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.repository.support;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.springframework.data.cassandra.mapping.CassandraPersistentEntity;
|
||||
import org.springframework.data.cassandra.mapping.CassandraPersistentProperty;
|
||||
import org.springframework.data.cassandra.repository.query.CassandraEntityInformation;
|
||||
import org.springframework.data.mapping.model.BeanWrapper;
|
||||
import org.springframework.data.repository.core.support.AbstractEntityInformation;
|
||||
|
||||
/**
|
||||
* {@link CassandraEntityInformation} implementation using a {@link CassandraPersistentEntity} instance to lookup the
|
||||
* necessary information. Can be configured with a custom collection to be returned which will trump the one returned by
|
||||
* the {@link CassandraPersistentEntity} if given.
|
||||
*
|
||||
* @author Alex Shvid
|
||||
*
|
||||
*/
|
||||
public class MappingCassandraEntityInformation<T, ID extends Serializable> extends AbstractEntityInformation<T, ID>
|
||||
implements CassandraEntityInformation<T, ID> {
|
||||
|
||||
private final CassandraPersistentEntity<T> entityMetadata;
|
||||
private final String customTableName;
|
||||
|
||||
/**
|
||||
* Creates a new {@link MappingCassandraEntityInformation} for the given {@link CassandraPersistentEntity}.
|
||||
*
|
||||
* @param entity must not be {@literal null}.
|
||||
*/
|
||||
public MappingCassandraEntityInformation(CassandraPersistentEntity<T> entity) {
|
||||
this(entity, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link MappingCassandraEntityInformation} for the given {@link CassandraPersistentEntity} and custom
|
||||
* table name.
|
||||
*
|
||||
* @param entity must not be {@literal null}.
|
||||
* @param customTableName
|
||||
*/
|
||||
public MappingCassandraEntityInformation(CassandraPersistentEntity<T> entity, String customTableName) {
|
||||
super(entity.getType());
|
||||
this.entityMetadata = entity;
|
||||
this.customTableName = customTableName;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.support.EntityInformation#getId(java.lang.Object)
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public ID getId(T entity) {
|
||||
|
||||
CassandraPersistentProperty idProperty = entityMetadata.getIdProperty();
|
||||
|
||||
if (idProperty == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return (ID) BeanWrapper.create(entity, null).getProperty(idProperty);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.support.EntityInformation#getIdType()
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Class<ID> getIdType() {
|
||||
return (Class<ID>) entityMetadata.getIdProperty().getType();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.mongodb.repository.CassandraEntityInformation#getTableName()
|
||||
*/
|
||||
@Override
|
||||
public String getTableName() {
|
||||
return customTableName == null ? entityMetadata.getTable() : customTableName;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.mongodb.repository.CassandraEntityInformation#getIdColumn()
|
||||
*/
|
||||
public String getIdColumn() {
|
||||
return entityMetadata.getIdProperty().getName();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,244 @@
|
||||
/*
|
||||
* Copyright 2010-2012 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.repository.support;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
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.cassandra.repository.query.CassandraEntityInformation;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* Repository base implementation for Cassandra.
|
||||
*
|
||||
* @author Alex Shvid
|
||||
*
|
||||
*/
|
||||
|
||||
public class SimpleCassandraRepository<T, ID extends Serializable> implements CassandraRepository<T, ID> {
|
||||
|
||||
private final CassandraOperations operations;
|
||||
private final CassandraDataOperations dataOperations;
|
||||
private final CassandraEntityInformation<T, ID> entityInformation;
|
||||
|
||||
/**
|
||||
* Creates a new {@link SimpleCassandraRepository} for the given {@link CassandraEntityInformation} and
|
||||
* {@link CassandraDataTemplate}.
|
||||
*
|
||||
* @param metadata must not be {@literal null}.
|
||||
* @param template must not be {@literal null}.
|
||||
*/
|
||||
public SimpleCassandraRepository(CassandraEntityInformation<T, ID> metadata, CassandraOperations operations,
|
||||
CassandraDataOperations dataOperations) {
|
||||
|
||||
Assert.notNull(operations);
|
||||
Assert.notNull(dataOperations);
|
||||
Assert.notNull(metadata);
|
||||
|
||||
this.entityInformation = metadata;
|
||||
this.operations = operations;
|
||||
this.dataOperations = dataOperations;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#save(java.lang.Object)
|
||||
*/
|
||||
public <S extends T> S save(S entity) {
|
||||
|
||||
Assert.notNull(entity, "Entity must not be null!");
|
||||
|
||||
// INSERT OR OPDATE?
|
||||
|
||||
dataOperations.update(entity, entityInformation.getTableName());
|
||||
return entity;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#save(java.lang.Iterable)
|
||||
*/
|
||||
public <S extends T> List<S> save(Iterable<S> entities) {
|
||||
|
||||
Assert.notNull(entities, "The given Iterable of entities not be null!");
|
||||
|
||||
List<S> result = new ArrayList<S>();
|
||||
|
||||
for (S entity : entities) {
|
||||
save(entity);
|
||||
result.add(entity);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private Clause getIdClause(ID id) {
|
||||
Clause clause = QueryBuilder.eq(entityInformation.getIdColumn(), id);
|
||||
return clause;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#findOne(java.io.Serializable)
|
||||
*/
|
||||
public T findOne(ID id) {
|
||||
Assert.notNull(id, "The given id must not be null!");
|
||||
|
||||
Select select = QueryBuilder.select().all().from(entityInformation.getTableName());
|
||||
select.where(getIdClause(id));
|
||||
|
||||
return dataOperations.selectOne(select, entityInformation.getJavaType());
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#exists(java.io.Serializable)
|
||||
*/
|
||||
public boolean exists(ID id) {
|
||||
|
||||
Assert.notNull(id, "The given id must not be null!");
|
||||
|
||||
Select select = QueryBuilder.select().countAll().from(entityInformation.getTableName());
|
||||
select.where(getIdClause(id));
|
||||
|
||||
Long num = dataOperations.count(select);
|
||||
return num != null && num.longValue() > 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#count()
|
||||
*/
|
||||
public long count() {
|
||||
return dataOperations.count(entityInformation.getTableName());
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#delete(java.io.Serializable)
|
||||
*/
|
||||
public void delete(ID id) {
|
||||
Assert.notNull(id, "The given id must not be null!");
|
||||
|
||||
Delete delete = QueryBuilder.delete().all().from(entityInformation.getTableName());
|
||||
delete.where(getIdClause(id));
|
||||
|
||||
operations.execute(delete.getQueryString());
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#delete(java.lang.Object)
|
||||
*/
|
||||
public void delete(T entity) {
|
||||
Assert.notNull(entity, "The given entity must not be null!");
|
||||
delete(entityInformation.getId(entity));
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#delete(java.lang.Iterable)
|
||||
*/
|
||||
public void delete(Iterable<? extends T> entities) {
|
||||
|
||||
Assert.notNull(entities, "The given Iterable of entities not be null!");
|
||||
|
||||
for (T entity : entities) {
|
||||
delete(entity);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#deleteAll()
|
||||
*/
|
||||
public void deleteAll() {
|
||||
Delete delete = QueryBuilder.delete().all().from(entityInformation.getTableName());
|
||||
operations.execute(delete.getQueryString());
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#findAll()
|
||||
*/
|
||||
public List<T> findAll() {
|
||||
Select select = QueryBuilder.select().all().from(entityInformation.getTableName());
|
||||
return findAll(select);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#findAll(java.lang.Iterable)
|
||||
*/
|
||||
public Iterable<T> findAll(Iterable<ID> ids) {
|
||||
|
||||
List<ID> parameters = new ArrayList<ID>();
|
||||
for (ID id : ids) {
|
||||
parameters.add(id);
|
||||
}
|
||||
Clause clause = QueryBuilder.in(entityInformation.getIdColumn(), parameters.toArray());
|
||||
Select select = QueryBuilder.select().all().from(entityInformation.getTableName());
|
||||
select.where(clause);
|
||||
|
||||
return findAll(select);
|
||||
}
|
||||
|
||||
private List<T> findAll(Select query) {
|
||||
|
||||
if (query == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
return dataOperations.select(query, entityInformation.getJavaType());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the underlying {@link CassandraOperations} instance.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
protected CassandraOperations getCassandraOperations() {
|
||||
return this.operations;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the underlying {@link CassandraDataOperations} instance.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
protected CassandraDataOperations getCassandraDataOperations() {
|
||||
return this.dataOperations;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the entityInformation
|
||||
*/
|
||||
protected CassandraEntityInformation<T, ID> getEntityInformation() {
|
||||
return entityInformation;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://java.sun.com/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
|
||||
</beans>
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
Profile.findByNamedQuery=SELECT firstName FROM table WHERE firstName=?0
|
||||
@@ -2,9 +2,11 @@
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cassandra="http://www.springframework.org/schema/data/cassandra"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:util="http://www.springframework.org/schema/util"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/data/cassandra http://www.springframework.org/schema/data/cassandra/spring-cassandra-1.0.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
|
||||
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
|
||||
|
||||
<context:property-placeholder
|
||||
location="classpath:/org/springframework/data/cassandra/test/integration/repository/cassandra.properties" />
|
||||
@@ -58,4 +60,17 @@
|
||||
<constructor-arg value="${cassandra.keyspace}" />
|
||||
</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="namedQueries">
|
||||
<bean class="org.springframework.data.repository.core.support.PropertiesBasedNamedQueries">
|
||||
<constructor-arg>
|
||||
<util:properties location="classpath:/META-INF/cassandra-named-queries.properties" />
|
||||
</constructor-arg>
|
||||
</bean>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
|
||||
Reference in New Issue
Block a user