DATACOUCH-329 - Save repo return with the generated id

Original pull request: #151.
This commit is contained in:
MLabusquiere
2017-08-08 08:56:24 +02:00
committed by Subhashni Balakrishnan
parent 5175bf23d8
commit 761c08517b
2 changed files with 100 additions and 1 deletions

View File

@@ -0,0 +1,93 @@
/*
* Copyright 2012-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.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.couchbase.repository;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.data.couchbase.IntegrationTestApplicationConfig;
import org.springframework.data.couchbase.core.mapping.Document;
import org.springframework.data.couchbase.core.mapping.id.GeneratedValue;
import org.springframework.data.couchbase.repository.config.RepositoryOperationsMapping;
import org.springframework.data.couchbase.repository.support.CouchbaseRepositoryFactory;
import org.springframework.data.couchbase.repository.support.IndexManager;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.springframework.data.couchbase.core.mapping.id.GenerationStrategy.UNIQUE;
/**
* @author Maxence Labusquiere
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = IntegrationTestApplicationConfig.class)
public class CouchbaseIdGenerationRepository {
@Autowired
private RepositoryOperationsMapping operationsMapping;
@Autowired
private IndexManager indexManager;
private CrudRepository<SimpleClassWithGeneratedIdValueUsingUUID, String> entityRepository;
@Before
public void setUp() throws Exception {
entityRepository = new CouchbaseRepositoryFactory(operationsMapping, indexManager).getRepository(EntityRepository.class);
}
@Test
public void idFieldEntityIsFillWithGeneratedValueOnSave() {
SimpleClassWithGeneratedIdValueUsingUUID entity = new SimpleClassWithGeneratedIdValueUsingUUID();
SimpleClassWithGeneratedIdValueUsingUUID savedEntity = entityRepository.save(entity);
assertThat("Expected generated value", savedEntity.id != null);
if (entityRepository.existsById(savedEntity.id)) {
entityRepository.existsById(savedEntity.id);
}
}
@Test
public void ifIdFieldIsAlreadySetNothingIsDone() {
String id = "AnId";
SimpleClassWithGeneratedIdValueUsingUUID entity = new SimpleClassWithGeneratedIdValueUsingUUID();
entity.setId(id);
SimpleClassWithGeneratedIdValueUsingUUID savedEntity = entityRepository.save(entity);
assertThat("Expected same id instance", savedEntity.id == id);
if (entityRepository.existsById(savedEntity.id)) {
entityRepository.existsById(savedEntity.id);
}
}
@Document
static class SimpleClassWithGeneratedIdValueUsingUUID {
@Id
@GeneratedValue(strategy = UNIQUE)
public String id;
public String value = "new";
public void setId(String id) {
this.id = id;
}
}
@Repository
interface EntityRepository extends CrudRepository<SimpleClassWithGeneratedIdValueUsingUUID, String> {
}
}

View File

@@ -591,7 +591,8 @@ public class CouchbaseTemplate implements CouchbaseOperations, ApplicationEventP
execute(new BucketCallback<Boolean>() {
@Override
public Boolean doInBucket() throws InterruptedException, ExecutionException {
converted.setId(addCommonPrefixAndSuffix(converted.getId()));
String generatedId = addCommonPrefixAndSuffix(converted.getId());
converted.setId(generatedId);
Document<String> doc = encodeAndWrap(converted, version);
Document<String> storedDoc;
//We will check version only if required
@@ -621,6 +622,11 @@ public class CouchbaseTemplate implements CouchbaseOperations, ApplicationEventP
storedDoc = client.insert(doc, persistTo, replicateTo);
break;
}
CouchbasePersistentProperty idProperty = persistentEntity.getIdProperty();
Object entityId = accessor.getProperty(idProperty);
if (!generatedId.equals(entityId)) {
accessor.setProperty(idProperty, generatedId);
}
if (storedDoc != null && storedDoc.cas() != 0) {
//inject new cas into the bean