DATACASS-469 - Remove CassandraOperations.selectBySimpleIds(…).
We no longer expose selectBySimpleIds(…) via CassandraOperations. selectBySimpleIds was a limited short-cut method that create a SELECT statement using the primary key name and using Id values as-is without applying type conversions. The replacement is to use a select(…) method accepting Query and an appropriate query:
select(query(where("id")).in("key", "other-key"), Person.class)
assuming the primary key property is id.
This commit is contained in:
@@ -244,16 +244,6 @@ public interface CassandraOperations {
|
||||
*/
|
||||
<T> T selectOneById(Object id, Class<T> entityClass) throws DataAccessException;
|
||||
|
||||
/**
|
||||
* Select objects for the given {@code entityClass} and {@code ids}.
|
||||
*
|
||||
* @param ids must not be {@literal null}.
|
||||
* @param entityClass The entity type must not be {@literal null}.
|
||||
* @return the converted results
|
||||
* @throws DataAccessException if there is any problem executing the query.
|
||||
*/
|
||||
<T> List<T> selectBySimpleIds(Iterable<?> ids, Class<T> entityClass) throws DataAccessException;
|
||||
|
||||
/**
|
||||
* Insert the given entity and return the entity if the insert was applied.
|
||||
*
|
||||
|
||||
@@ -15,6 +15,9 @@
|
||||
*/
|
||||
package org.springframework.data.cassandra.core;
|
||||
|
||||
import lombok.NonNull;
|
||||
import lombok.Value;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
@@ -22,9 +25,6 @@ import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import lombok.NonNull;
|
||||
import lombok.Value;
|
||||
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.data.cassandra.SessionFactory;
|
||||
import org.springframework.data.cassandra.core.convert.CassandraConverter;
|
||||
@@ -399,29 +399,6 @@ public class CassandraTemplate implements CassandraOperations {
|
||||
return selectOne(select, entityClass);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> List<T> selectBySimpleIds(Iterable<?> ids, Class<T> entityClass) throws DataAccessException {
|
||||
|
||||
Assert.notNull(ids, "Ids must not be null");
|
||||
Assert.notNull(entityClass, "EntityClass must not be null");
|
||||
|
||||
CassandraPersistentEntity<?> entity = getMappingContext().getRequiredPersistentEntity(entityClass);
|
||||
|
||||
CassandraPersistentProperty idProperty = entity.getRequiredIdProperty();
|
||||
|
||||
if (idProperty.isCompositePrimaryKey()) {
|
||||
throw new IllegalArgumentException(String.format(
|
||||
"Entity class [%s] uses a composite primary key class [%s] which this method can't support",
|
||||
entityClass.getName(), idProperty.getType().getName()));
|
||||
}
|
||||
|
||||
Select select = QueryBuilder.select().all().from(entity.getTableName().toCql());
|
||||
|
||||
select.where(QueryBuilder.in(idProperty.getColumnName().toCql(), toList(ids)));
|
||||
|
||||
return select(select, entityClass);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.cassandra.core.CassandraOperations#insert(java.lang.Object)
|
||||
|
||||
@@ -25,4 +25,11 @@ import org.springframework.data.repository.core.EntityInformation;
|
||||
*/
|
||||
public interface CassandraEntityInformation<T, ID> extends EntityInformation<T, ID>, CassandraEntityMetadata<T> {
|
||||
|
||||
/**
|
||||
* Returns the attribute that the id will be persisted to.
|
||||
*
|
||||
* @return
|
||||
* @since 2.0
|
||||
*/
|
||||
String getIdAttribute();
|
||||
}
|
||||
|
||||
@@ -83,6 +83,14 @@ public class MappingCassandraEntityInformation<T, ID> extends AbstractEntityInfo
|
||||
return (Class<ID>) MapId.class;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.cassandra.repository.query.CassandraEntityInformation#getIdAttribute()
|
||||
*/
|
||||
@Override
|
||||
public String getIdAttribute() {
|
||||
return entityMetadata.getRequiredIdProperty().getName();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.cassandra.repository.query.CassandraEntityMetadata#getTableName()
|
||||
*/
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.springframework.data.cassandra.repository.support;
|
||||
|
||||
import static org.springframework.data.cassandra.core.query.Criteria.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
@@ -26,8 +28,11 @@ import org.springframework.data.cassandra.core.CassandraOperations;
|
||||
import org.springframework.data.cassandra.core.CassandraTemplate;
|
||||
import org.springframework.data.cassandra.core.convert.CassandraConverter;
|
||||
import org.springframework.data.cassandra.core.mapping.CassandraPersistentEntity;
|
||||
import org.springframework.data.cassandra.core.query.Query;
|
||||
import org.springframework.data.cassandra.repository.CassandraRepository;
|
||||
import org.springframework.data.cassandra.repository.query.CassandraEntityInformation;
|
||||
import org.springframework.data.util.StreamUtils;
|
||||
import org.springframework.data.util.Streamable;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import com.datastax.driver.core.querybuilder.Insert;
|
||||
@@ -101,8 +106,8 @@ public class SimpleCassandraRepository<T, ID> implements CassandraRepository<T,
|
||||
|
||||
CassandraConverter converter = operations.getConverter();
|
||||
|
||||
CassandraPersistentEntity<?> persistentEntity =
|
||||
converter.getMappingContext().getRequiredPersistentEntity(entity.getClass());
|
||||
CassandraPersistentEntity<?> persistentEntity = converter.getMappingContext()
|
||||
.getRequiredPersistentEntity(entity.getClass());
|
||||
|
||||
Map<String, Object> toInsert = new LinkedHashMap<>();
|
||||
|
||||
@@ -197,7 +202,10 @@ public class SimpleCassandraRepository<T, ID> implements CassandraRepository<T,
|
||||
|
||||
Assert.notNull(ids, "The given Iterable of id's must not be null");
|
||||
|
||||
return operations.selectBySimpleIds(ids, entityInformation.getJavaType());
|
||||
List<ID> idCollection = Streamable.of(ids).stream().collect(StreamUtils.toUnmodifiableList());
|
||||
|
||||
return operations.select(Query.query(where(entityInformation.getIdAttribute()).in(idCollection)),
|
||||
entityInformation.getJavaType());
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
|
||||
Reference in New Issue
Block a user