diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/TypedIdCassandraRepository.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/TypedIdCassandraRepository.java index a967c3b15..b3f7e94e3 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/TypedIdCassandraRepository.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/TypedIdCassandraRepository.java @@ -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; *
  • Define your repository interface to be a subinterface of this interface, including your entity type and * your primary key class type.
  • * - *
  • - * Strategy: embed identity fields or properties directly in your entity and use {@link CassandraRepository}
  • + *
  • Strategy: embed identity fields or properties directly in your entity and use + * {@link CassandraRepository}
  • * * - * + * * @author Alex Shvid * @author Matthew T. Adams + * @author Mark Paluch */ @NoRepositoryBean -public interface TypedIdCassandraRepository extends CrudRepository {} +public interface TypedIdCassandraRepository extends CrudRepository { + + /** + * 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 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 + */ + List insert(Iterable entities); + +} diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/support/SimpleCassandraRepository.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/support/SimpleCassandraRepository.java index d31fd952a..32426b3f7 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/support/SimpleCassandraRepository.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/support/SimpleCassandraRepository.java @@ -95,6 +95,39 @@ public class SimpleCassandraRepository implements Ty return result; } + /* (non-Javadoc) + * @see org.springframework.data.cassandra.repository.TypedIdCassandraRepository#insert(java.lang.Object) + */ + @Override + public 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 List insert(Iterable entities) { + + Assert.notNull(entities, "The given Iterable of entities must not be null"); + + List 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 implements Ty * @see org.springframework.data.repository.CrudRepository#findAll(java.lang.Iterable) */ @Override - public Iterable findAll(Iterable iterable) { + public List findAll(Iterable 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) diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/repository/support/SimpleCassandraRepositoryIntegrationTests.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/repository/support/SimpleCassandraRepositoryIntegrationTests.java index dd43bdb36..96378fd12 100644 --- a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/repository/support/SimpleCassandraRepositoryIntegrationTests.java +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/repository/support/SimpleCassandraRepositoryIntegrationTests.java @@ -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() {