Skip to content

Commit 239ce94

Browse files
committed
DATACOUCH-626 - Handle reading into entity with only all-args constructor; version primative type.
The version arg - which is not in the document - will be passed into the constructor as a null which will fail if it is a primitive type. This change will pre-populated the converted object with the version/cas, so that it is not null.
1 parent 3cccf1f commit 239ce94

File tree

5 files changed

+146
-1
lines changed

5 files changed

+146
-1
lines changed

src/main/java/org/springframework/data/couchbase/core/CouchbaseTemplateSupport.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,13 @@ public CouchbaseDocument encodeEntity(final Object entityToEncode) {
7575
public <T> T decodeEntity(String id, String source, long cas, Class<T> entityClass) {
7676
final CouchbaseDocument converted = new CouchbaseDocument(id);
7777
converted.setId(id);
78+
CouchbasePersistentEntity<?> persistentEntity = mappingContext.getRequiredPersistentEntity(entityClass);
79+
if (cas != 0 && persistentEntity.getVersionProperty() != null) {
80+
converted.put(persistentEntity.getVersionProperty().getName(), cas);
81+
}
7882

7983
T readEntity = converter.read(entityClass, (CouchbaseDocument) translationService.decode(source, converted));
8084
final ConvertingPropertyAccessor<T> accessor = getPropertyAccessor(readEntity);
81-
CouchbasePersistentEntity<?> persistentEntity = mappingContext.getRequiredPersistentEntity(readEntity.getClass());
8285

8386
if (persistentEntity.getVersionProperty() != null) {
8487
accessor.setProperty(persistentEntity.getVersionProperty(), cas);

src/main/java/org/springframework/data/couchbase/repository/support/Util.java

+21
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
1+
/*
2+
* Copyright 2020 the original author or authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package org.springframework.data.couchbase.repository.support;
218

319
import org.springframework.data.couchbase.core.convert.CouchbaseConverter;
420
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentEntity;
521
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentProperty;
622

23+
/**
24+
* Utility class for Couchbase Repository
25+
*
26+
* @author Michael Reiche
27+
*/
728
public class Util {
829

930
public static boolean hasNonZeroVersionProperty(Object entity, CouchbaseConverter converter) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2020 the original author or authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.data.couchbase.domain;
18+
19+
import lombok.EqualsAndHashCode;
20+
import lombok.Getter;
21+
import lombok.ToString;
22+
import org.springframework.data.annotation.Id;
23+
import org.springframework.data.annotation.Version;
24+
import org.springframework.data.couchbase.core.mapping.Document;
25+
import org.springframework.data.couchbase.core.mapping.Field;
26+
import org.springframework.data.couchbase.core.mapping.id.GeneratedValue;
27+
import org.springframework.data.couchbase.core.mapping.id.GenerationStrategy;
28+
29+
/**
30+
* SubscriptionTokenEntity for tests
31+
*
32+
* @author Michael Reiche
33+
*/
34+
@Getter
35+
@ToString
36+
@EqualsAndHashCode
37+
@Document
38+
public class SubscriptionToken {
39+
private @Id
40+
@GeneratedValue(strategy = GenerationStrategy.UNIQUE)
41+
String id;
42+
private @Version
43+
long version;
44+
private @Field
45+
String subscriptionType;
46+
private @Field
47+
String userName;
48+
private @Field
49+
String appId;
50+
private @Field
51+
String deviceId;
52+
private @Field
53+
long subscriptionDate;
54+
55+
public SubscriptionToken(
56+
String id,
57+
long version,
58+
String subscriptionType,
59+
String userName,
60+
String appId,
61+
String deviceId,
62+
long subscriptionDate) {
63+
this.id = id;
64+
this.version = version;
65+
this.subscriptionType = subscriptionType;
66+
this.userName = userName;
67+
this.appId = appId;
68+
this.deviceId = deviceId;
69+
this.subscriptionDate = subscriptionDate;
70+
}
71+
72+
public void setType(String type) {
73+
type = type;
74+
}
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2020 the original author or authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.data.couchbase.domain;
18+
19+
import org.springframework.data.repository.PagingAndSortingRepository;
20+
import org.springframework.stereotype.Repository;
21+
22+
/**
23+
* SubscriptionToken Repository for tests
24+
*
25+
* @author Michael Reiche
26+
*/
27+
@Repository
28+
public interface SubscriptionTokenRepository extends PagingAndSortingRepository<SubscriptionToken, String> {
29+
}

src/test/java/org/springframework/data/couchbase/repository/CouchbaseRepositoryKeyValueIntegrationTests.java

+17
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,18 @@
2424
import java.util.Optional;
2525
import java.util.UUID;
2626

27+
import com.couchbase.client.java.kv.GetResult;
2728
import org.junit.jupiter.api.Test;
2829
import org.springframework.beans.factory.annotation.Autowired;
2930
import org.springframework.context.annotation.Configuration;
3031
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
32+
import org.springframework.data.couchbase.core.CouchbaseTemplate;
3133
import org.springframework.data.couchbase.domain.Course;
3234
import org.springframework.data.couchbase.domain.Library;
3335
import org.springframework.data.couchbase.domain.LibraryRepository;
3436
import org.springframework.data.couchbase.domain.Submission;
37+
import org.springframework.data.couchbase.domain.SubscriptionToken;
38+
import org.springframework.data.couchbase.domain.SubscriptionTokenRepository;
3539
import org.springframework.data.couchbase.domain.User;
3640
import org.springframework.data.couchbase.domain.UserRepository;
3741
import org.springframework.data.couchbase.domain.UserSubmission;
@@ -54,7 +58,20 @@ public class CouchbaseRepositoryKeyValueIntegrationTests extends ClusterAwareInt
5458

5559
@Autowired UserRepository userRepository;
5660
@Autowired LibraryRepository libraryRepository;
61+
@Autowired SubscriptionTokenRepository subscriptionTokenRepository;
5762
@Autowired UserSubmissionRepository userSubmissionRepository;
63+
@Autowired CouchbaseTemplate couchbaseTemplate;
64+
65+
@Test
66+
void subscriptionToken() {
67+
SubscriptionToken st = new SubscriptionToken("id", 0, "type", "Dave Smith", "app123", "dev123", 0);
68+
st = subscriptionTokenRepository.save(st);
69+
st = subscriptionTokenRepository.findById(st.getId()).get();
70+
71+
GetResult jdkResult = couchbaseTemplate.getCouchbaseClientFactory().getDefaultCollection().get(st.getId());
72+
assertNotEquals(0, st.getVersion());
73+
assertEquals(jdkResult.cas(), st.getVersion());
74+
}
5875

5976
@Test
6077
@IgnoreWhen(clusterTypes = ClusterType.MOCKED)

0 commit comments

Comments
 (0)