Merge branch 'DATACASS-32' of https://github.com/shvid/spring-data-cassandra into DATACASS-32
This commit is contained in:
@@ -127,9 +127,7 @@ public abstract class AbstractCassandraConfiguration implements BeanClassLoaderA
|
||||
*/
|
||||
@Bean
|
||||
public CassandraOperations cassandraTemplate() throws Exception {
|
||||
CassandraTemplate template = new CassandraTemplate(keyspace());
|
||||
template.setBeanClassLoader(beanClassLoader);
|
||||
return template;
|
||||
return new CassandraTemplate(keyspace());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -140,9 +138,7 @@ public abstract class AbstractCassandraConfiguration implements BeanClassLoaderA
|
||||
*/
|
||||
@Bean
|
||||
public CassandraAdminOperations cassandraAdminTemplate() throws Exception {
|
||||
CassandraAdminTemplate adminTemplate = new CassandraAdminTemplate(keyspace());
|
||||
adminTemplate.setBeanClassLoader(beanClassLoader);
|
||||
return adminTemplate;
|
||||
return new CassandraAdminTemplate(keyspace());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -38,6 +38,7 @@ import org.springframework.data.util.TypeInformation;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
import com.datastax.driver.core.Row;
|
||||
import com.datastax.driver.core.querybuilder.Delete.Where;
|
||||
import com.datastax.driver.core.querybuilder.Insert;
|
||||
import com.datastax.driver.core.querybuilder.QueryBuilder;
|
||||
import com.datastax.driver.core.querybuilder.Update;
|
||||
@@ -182,6 +183,8 @@ public class MappingCassandraConverter extends AbstractCassandraConverter implem
|
||||
writeInsertInternal(obj, (Insert) builtStatement, entity);
|
||||
} else if (builtStatement instanceof Update) {
|
||||
writeUpdateInternal(obj, (Update) builtStatement, entity);
|
||||
} else if (builtStatement instanceof Where) {
|
||||
writeDeleteWhereInternal(obj, (Where) builtStatement, entity);
|
||||
} else {
|
||||
throw new MappingException("Unknown buildStatement " + builtStatement.getClass().getName());
|
||||
}
|
||||
@@ -231,6 +234,30 @@ public class MappingCassandraConverter extends AbstractCassandraConverter implem
|
||||
|
||||
}
|
||||
|
||||
private void writeDeleteWhereInternal(final Object objectToSave, final Where whereId,
|
||||
CassandraPersistentEntity<?> entity) {
|
||||
|
||||
final BeanWrapper<CassandraPersistentEntity<Object>, Object> wrapper = BeanWrapper.create(objectToSave,
|
||||
conversionService);
|
||||
|
||||
// Write the properties
|
||||
entity.doWithProperties(new PropertyHandler<CassandraPersistentProperty>() {
|
||||
public void doWithPersistentProperty(CassandraPersistentProperty prop) {
|
||||
|
||||
if (prop.isIdProperty()) {
|
||||
|
||||
Object propertyObj = wrapper.getProperty(prop, prop.getType(), useFieldAccessOnly);
|
||||
|
||||
if (propertyObj != null) {
|
||||
whereId.and(QueryBuilder.eq(prop.getColumnName(), propertyObj));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> Class<T> transformClassToBeanClassLoaderClass(Class<T> entity) {
|
||||
try {
|
||||
|
||||
@@ -5,7 +5,6 @@ import java.util.Map;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.BeanClassLoaderAware;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
import org.springframework.dao.support.PersistenceExceptionTranslator;
|
||||
@@ -24,7 +23,7 @@ import com.datastax.driver.core.TableMetadata;
|
||||
/**
|
||||
* Default implementation of {@link CassandraAdminOperations}.
|
||||
*/
|
||||
public class CassandraAdminTemplate implements CassandraAdminOperations, BeanClassLoaderAware {
|
||||
public class CassandraAdminTemplate implements CassandraAdminOperations {
|
||||
|
||||
private static Logger log = LoggerFactory.getLogger(CassandraAdminTemplate.class);
|
||||
|
||||
@@ -35,8 +34,6 @@ public class CassandraAdminTemplate implements CassandraAdminOperations, BeanCla
|
||||
|
||||
private final PersistenceExceptionTranslator exceptionTranslator = new CassandraExceptionTranslator();
|
||||
|
||||
private ClassLoader beanClassLoader;
|
||||
|
||||
/**
|
||||
* Constructor used for a basic template configuration
|
||||
*
|
||||
@@ -241,8 +238,4 @@ public class CassandraAdminTemplate implements CassandraAdminOperations, BeanCla
|
||||
return entity.getTable();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBeanClassLoader(ClassLoader classLoader) {
|
||||
this.beanClassLoader = classLoader;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,6 @@ import java.util.Set;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.BeanClassLoaderAware;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.dao.DuplicateKeyException;
|
||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
@@ -41,7 +40,6 @@ import org.springframework.data.cassandra.util.CqlUtils;
|
||||
import org.springframework.data.convert.EntityReader;
|
||||
import org.springframework.data.mapping.context.MappingContext;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
import com.datastax.driver.core.Host;
|
||||
import com.datastax.driver.core.Metadata;
|
||||
@@ -59,7 +57,7 @@ import com.datastax.driver.core.querybuilder.Batch;
|
||||
* @author Alex Shvid
|
||||
* @author David Webb
|
||||
*/
|
||||
public class CassandraTemplate implements CassandraOperations, BeanClassLoaderAware {
|
||||
public class CassandraTemplate implements CassandraOperations {
|
||||
|
||||
/**
|
||||
* Simple {@link RowCallback} that will transform {@link Row} into the given target type using the given
|
||||
@@ -105,8 +103,6 @@ public class CassandraTemplate implements CassandraOperations, BeanClassLoaderAw
|
||||
private final MappingContext<? extends CassandraPersistentEntity<?>, CassandraPersistentProperty> mappingContext;
|
||||
private final PersistenceExceptionTranslator exceptionTranslator = new CassandraExceptionTranslator();
|
||||
|
||||
private ClassLoader beanClassLoader;
|
||||
|
||||
/**
|
||||
* Constructor used for a basic template configuration
|
||||
*
|
||||
@@ -720,13 +716,6 @@ public class CassandraTemplate implements CassandraOperations, BeanClassLoaderAw
|
||||
return selectOneInternal(query, new ReadRowCallback<T>(cassandraConverter, selectClass));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param classLoader
|
||||
*/
|
||||
public void setBeanClassLoader(ClassLoader classLoader) {
|
||||
this.beanClassLoader = classLoader;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.cassandra.core.CassandraOperations#update(java.util.List)
|
||||
*/
|
||||
@@ -985,13 +974,10 @@ public class CassandraTemplate implements CassandraOperations, BeanClassLoaderAw
|
||||
|
||||
Assert.notEmpty(entities);
|
||||
|
||||
CassandraPersistentEntity<?> CPEntity = getEntity(entities.get(0));
|
||||
|
||||
Assert.notNull(CPEntity);
|
||||
|
||||
try {
|
||||
|
||||
final Batch b = CqlUtils.toDeleteBatchQuery(keyspace.getKeyspace(), tableName, entities, CPEntity, optionsByName);
|
||||
final Batch b = CqlUtils.toDeleteBatchQuery(keyspace.getKeyspace(), tableName, entities, optionsByName,
|
||||
cassandraConverter);
|
||||
log.info(b.toString());
|
||||
|
||||
execute(new SessionCallback<Object>() {
|
||||
@@ -1109,13 +1095,10 @@ public class CassandraTemplate implements CassandraOperations, BeanClassLoaderAw
|
||||
protected <T> void doDelete(final String tableName, final T objectToRemove, Map<String, Object> optionsByName,
|
||||
final boolean deleteAsynchronously) {
|
||||
|
||||
CassandraPersistentEntity<?> entity = getEntity(objectToRemove);
|
||||
|
||||
Assert.notNull(entity);
|
||||
|
||||
try {
|
||||
|
||||
final Query q = CqlUtils.toDeleteQuery(keyspace.getKeyspace(), tableName, objectToRemove, entity, optionsByName);
|
||||
final Query q = CqlUtils.toDeleteQuery(keyspace.getKeyspace(), tableName, objectToRemove, optionsByName,
|
||||
cassandraConverter);
|
||||
log.info(q.toString());
|
||||
|
||||
execute(new SessionCallback<Object>() {
|
||||
@@ -1257,30 +1240,6 @@ public class CassandraTemplate implements CassandraOperations, BeanClassLoaderAw
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines the PersistentEntityType for a given Object
|
||||
*
|
||||
* @param o
|
||||
* @return
|
||||
*/
|
||||
protected CassandraPersistentEntity<?> getEntity(Object o) {
|
||||
|
||||
CassandraPersistentEntity<?> entity = null;
|
||||
try {
|
||||
String entityClassName = o.getClass().getName();
|
||||
Class<?> entityClass = ClassUtils.forName(entityClassName, beanClassLoader);
|
||||
entity = mappingContext.getPersistentEntity(entityClass);
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (LinkageError e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
}
|
||||
|
||||
return entity;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param query
|
||||
* @param readRowCallback
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package org.springframework.data.cassandra.util;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -353,45 +352,16 @@ public abstract class CqlUtils {
|
||||
* @throws EntityWriterException
|
||||
*/
|
||||
public static Query toDeleteQuery(String keyspace, String tableName, final Object objectToRemove,
|
||||
CassandraPersistentEntity<?> entity, Map<String, Object> optionsByName) throws EntityWriterException {
|
||||
Map<String, Object> optionsByName, EntityWriter<Object, Object> entityWriter) throws EntityWriterException {
|
||||
|
||||
final Delete.Selection ds = QueryBuilder.delete();
|
||||
final Delete q = ds.from(keyspace, tableName);
|
||||
final Where w = q.where();
|
||||
|
||||
final Exception innerException = new Exception();
|
||||
|
||||
entity.doWithProperties(new PropertyHandler<CassandraPersistentProperty>() {
|
||||
public void doWithPersistentProperty(CassandraPersistentProperty prop) {
|
||||
|
||||
/*
|
||||
* See if the object has a value for that column, and if so, add it to the Query
|
||||
*/
|
||||
try {
|
||||
|
||||
if (prop.isIdProperty()) {
|
||||
Object o = (String) prop.getGetter().invoke(objectToRemove, new Object[0]);
|
||||
|
||||
log.info("Getter Invoke [" + prop.getColumnName() + " => " + o);
|
||||
|
||||
if (o != null) {
|
||||
w.and(QueryBuilder.eq(prop.getColumnName(), o));
|
||||
}
|
||||
}
|
||||
|
||||
} catch (IllegalAccessException e) {
|
||||
innerException.initCause(e);
|
||||
} catch (IllegalArgumentException e) {
|
||||
innerException.initCause(e);
|
||||
} catch (InvocationTargetException e) {
|
||||
innerException.initCause(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (innerException.getCause() != null) {
|
||||
throw new EntityWriterException("Failed to convert Persistent Entity to CQL/Query", innerException.getCause());
|
||||
}
|
||||
/*
|
||||
* Write where condition to find by Id
|
||||
*/
|
||||
entityWriter.write(objectToRemove, w);
|
||||
|
||||
addQueryOptions(q, optionsByName);
|
||||
|
||||
@@ -449,7 +419,7 @@ public abstract class CqlUtils {
|
||||
* @throws EntityWriterException
|
||||
*/
|
||||
public static <T> Batch toDeleteBatchQuery(String keyspaceName, String tableName, List<T> entities,
|
||||
CassandraPersistentEntity<?> entity, Map<String, Object> optionsByName) throws EntityWriterException {
|
||||
Map<String, Object> optionsByName, EntityWriter<Object, Object> entityWriter) throws EntityWriterException {
|
||||
|
||||
/*
|
||||
* Return variable is a Batch statement
|
||||
@@ -458,7 +428,7 @@ public abstract class CqlUtils {
|
||||
|
||||
for (final T objectToSave : entities) {
|
||||
|
||||
b.add((Statement) toDeleteQuery(keyspaceName, tableName, objectToSave, entity, optionsByName));
|
||||
b.add((Statement) toDeleteQuery(keyspaceName, tableName, objectToSave, optionsByName, entityWriter));
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user