DATACOUCH-329 - Save repo return with the generated id
Original pull request: #151.
This commit is contained in:
committed by
Subhashni Balakrishnan
parent
3499d41fee
commit
8a34eb82c1
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* 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 mlabusquiere
|
||||
*/
|
||||
@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.exists(savedEntity.id)) {
|
||||
entityRepository.delete(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.exists(savedEntity.id)) {
|
||||
entityRepository.delete(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> {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -586,7 +586,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
|
||||
@@ -617,6 +618,11 @@ public class CouchbaseTemplate implements CouchbaseOperations, ApplicationEventP
|
||||
break;
|
||||
}
|
||||
|
||||
CouchbasePersistentProperty idProperty = persistentEntity.getIdProperty();
|
||||
Object entityId = accessor.getProperty(idProperty);
|
||||
if (!generatedId.equals(entityId)) {
|
||||
accessor.setProperty(idProperty, generatedId);
|
||||
}
|
||||
if (persistentEntity.hasVersionProperty() && storedDoc != null && storedDoc.cas() != 0) {
|
||||
//inject new cas into the bean
|
||||
accessor.setProperty(versionProperty, storedDoc.cas());
|
||||
|
||||
Reference in New Issue
Block a user