DATACASS-415 - Provide insert methods on TypedIdCassandraRepository.
We now provide insert(…) methods on TypedIdCassandraRepository for fine-grained control over insert/update behavior.
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
/*
|
||||
* Copyright 2013-2014 the original author or authors
|
||||
*
|
||||
* Copyright 2013-2017 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.
|
||||
@@ -16,6 +16,7 @@
|
||||
package org.springframework.data.cassandra.repository;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.cassandra.mapping.PrimaryKey;
|
||||
import org.springframework.data.cassandra.mapping.PrimaryKeyClass;
|
||||
@@ -48,8 +49,8 @@ import org.springframework.data.repository.NoRepositoryBean;
|
||||
* <li>Define your repository interface to be a subinterface of <em>this</em> interface, including your entity type and
|
||||
* your primary key class type.</li>
|
||||
* </ul>
|
||||
* <li>
|
||||
* <em>Strategy: embed identity fields or properties directly in your entity and use {@link CassandraRepository}</em></li>
|
||||
* <li><em>Strategy: embed identity fields or properties directly in your entity and use
|
||||
* {@link CassandraRepository}</em></li>
|
||||
* <ul>
|
||||
* <li>Define your entity, including a field or property for each column, including those for partition and (optional)
|
||||
* cluster columns.</li>
|
||||
@@ -61,9 +62,34 @@ import org.springframework.data.repository.NoRepositoryBean;
|
||||
* construct an id.</li>
|
||||
* </ul>
|
||||
* </ul>
|
||||
*
|
||||
*
|
||||
* @author Alex Shvid
|
||||
* @author Matthew T. Adams
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@NoRepositoryBean
|
||||
public interface TypedIdCassandraRepository<T, ID extends Serializable> extends CrudRepository<T, ID> {}
|
||||
public interface TypedIdCassandraRepository<T, ID extends Serializable> extends CrudRepository<T, ID> {
|
||||
|
||||
/**
|
||||
* Inserts the given entity. Assumes the instance to be new to be able to apply insertion optimizations. Use the
|
||||
* returned instance for further operations as the save operation might have changed the entity instance completely.
|
||||
* Prefer using {@link #save(Object)} instead to avoid the usage of store-specific API.
|
||||
*
|
||||
* @param entity must not be {@literal null}.
|
||||
* @return the saved entity
|
||||
* @since 2.0
|
||||
*/
|
||||
<S extends T> S insert(S entity);
|
||||
|
||||
/**
|
||||
* Inserts the given entities. Assumes the given entities to have not been persisted yet and thus will optimize the
|
||||
* insert over a call to {@link #save(Iterable)}. Prefer using {@link #save(Iterable)} to avoid the usage of store
|
||||
* specific API.
|
||||
*
|
||||
* @param entities must not be {@literal null}.
|
||||
* @return the saved entities
|
||||
* @since 2.0
|
||||
*/
|
||||
<S extends T> List<S> insert(Iterable<S> entities);
|
||||
|
||||
}
|
||||
|
||||
@@ -95,6 +95,39 @@ public class SimpleCassandraRepository<T, ID extends Serializable> implements Ty
|
||||
return result;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.cassandra.repository.TypedIdCassandraRepository#insert(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public <S extends T> S insert(S entity) {
|
||||
|
||||
Assert.notNull(entity, "Entity must not be null");
|
||||
|
||||
return operations.insert(entity);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.cassandra.repository.TypedIdCassandraRepository#insert(java.lang.Iterable)
|
||||
*/
|
||||
@Override
|
||||
public <S extends T> List<S> insert(Iterable<S> entities) {
|
||||
|
||||
Assert.notNull(entities, "The given Iterable of entities must not be null");
|
||||
|
||||
List<S> result = new ArrayList<>();
|
||||
|
||||
for (S entity : entities) {
|
||||
|
||||
S saved = operations.insert(entity);
|
||||
|
||||
if (saved != null) {
|
||||
result.add(saved);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#findOne(java.io.Serializable)
|
||||
*/
|
||||
@@ -140,11 +173,11 @@ public class SimpleCassandraRepository<T, ID extends Serializable> implements Ty
|
||||
* @see org.springframework.data.repository.CrudRepository#findAll(java.lang.Iterable)
|
||||
*/
|
||||
@Override
|
||||
public Iterable<T> findAll(Iterable<ID> iterable) {
|
||||
public List<T> findAll(Iterable<ID> ids) {
|
||||
|
||||
Assert.notNull(iterable, "The given Iterable of id's must not be null");
|
||||
Assert.notNull(ids, "The given Iterable of id's must not be null");
|
||||
|
||||
return operations.selectBySimpleIds(iterable, entityInformation.getJavaType());
|
||||
return operations.selectBySimpleIds(ids, entityInformation.getJavaType());
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
|
||||
@@ -160,6 +160,28 @@ public class SimpleCassandraRepositoryIntegrationTests extends AbstractKeyspaceC
|
||||
assertThat(count).isEqualTo(4);
|
||||
}
|
||||
|
||||
@Test // DATACASS-415
|
||||
public void insertEntityShouldInsertEntity() {
|
||||
|
||||
repository.deleteAll();
|
||||
|
||||
Person person = new Person("36", "Homer", "Simpson");
|
||||
|
||||
repository.insert(person);
|
||||
|
||||
assertThat(repository.count()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test // DATACASS-415
|
||||
public void insertIterableOfEntitiesShouldInsertEntity() {
|
||||
|
||||
repository.deleteAll();
|
||||
|
||||
repository.insert(Arrays.asList(dave, oliver, boyd));
|
||||
|
||||
assertThat(repository.count()).isEqualTo(3);
|
||||
}
|
||||
|
||||
@Test // DATACASS-396
|
||||
public void saveEntityShouldUpdateExistingEntity() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user