Skip to content

DATACOUCH-626 - Handle reading into entity with only all-args constru… #272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,13 @@ public CouchbaseDocument encodeEntity(final Object entityToEncode) {
public <T> T decodeEntity(String id, String source, long cas, Class<T> entityClass) {
final CouchbaseDocument converted = new CouchbaseDocument(id);
converted.setId(id);
CouchbasePersistentEntity<?> persistentEntity = mappingContext.getRequiredPersistentEntity(entityClass);
if (cas != 0 && persistentEntity.getVersionProperty() != null) {
converted.put(persistentEntity.getVersionProperty().getName(), cas);
}

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

if (persistentEntity.getVersionProperty() != null) {
accessor.setProperty(persistentEntity.getVersionProperty(), cas);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
/*
* Copyright 2020 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
*
* https://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.support;

import org.springframework.data.couchbase.core.convert.CouchbaseConverter;
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentEntity;
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentProperty;

/**
* Utility class for Couchbase Repository
*
* @author Michael Reiche
*/
public class Util {

public static boolean hasNonZeroVersionProperty(Object entity, CouchbaseConverter converter) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2020 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
*
* https://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.domain;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Version;
import org.springframework.data.couchbase.core.mapping.Document;
import org.springframework.data.couchbase.core.mapping.Field;
import org.springframework.data.couchbase.core.mapping.id.GeneratedValue;
import org.springframework.data.couchbase.core.mapping.id.GenerationStrategy;

/**
* SubscriptionTokenEntity for tests
*
* @author Michael Reiche
*/
@Getter
@ToString
@EqualsAndHashCode
@Document
public class SubscriptionToken {
private @Id
@GeneratedValue(strategy = GenerationStrategy.UNIQUE)
String id;
private @Version
long version;
private @Field
String subscriptionType;
private @Field
String userName;
private @Field
String appId;
private @Field
String deviceId;
private @Field
long subscriptionDate;

public SubscriptionToken(
String id,
long version,
String subscriptionType,
String userName,
String appId,
String deviceId,
long subscriptionDate) {
this.id = id;
this.version = version;
this.subscriptionType = subscriptionType;
this.userName = userName;
this.appId = appId;
this.deviceId = deviceId;
this.subscriptionDate = subscriptionDate;
}

public void setType(String type) {
type = type;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2020 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
*
* https://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.domain;

import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;

/**
* SubscriptionToken Repository for tests
*
* @author Michael Reiche
*/
@Repository
public interface SubscriptionTokenRepository extends PagingAndSortingRepository<SubscriptionToken, String> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,18 @@
import java.util.Optional;
import java.util.UUID;

import com.couchbase.client.java.kv.GetResult;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
import org.springframework.data.couchbase.core.CouchbaseTemplate;
import org.springframework.data.couchbase.domain.Course;
import org.springframework.data.couchbase.domain.Library;
import org.springframework.data.couchbase.domain.LibraryRepository;
import org.springframework.data.couchbase.domain.Submission;
import org.springframework.data.couchbase.domain.SubscriptionToken;
import org.springframework.data.couchbase.domain.SubscriptionTokenRepository;
import org.springframework.data.couchbase.domain.User;
import org.springframework.data.couchbase.domain.UserRepository;
import org.springframework.data.couchbase.domain.UserSubmission;
Expand All @@ -54,7 +58,20 @@ public class CouchbaseRepositoryKeyValueIntegrationTests extends ClusterAwareInt

@Autowired UserRepository userRepository;
@Autowired LibraryRepository libraryRepository;
@Autowired SubscriptionTokenRepository subscriptionTokenRepository;
@Autowired UserSubmissionRepository userSubmissionRepository;
@Autowired CouchbaseTemplate couchbaseTemplate;

@Test
void subscriptionToken() {
SubscriptionToken st = new SubscriptionToken("id", 0, "type", "Dave Smith", "app123", "dev123", 0);
st = subscriptionTokenRepository.save(st);
st = subscriptionTokenRepository.findById(st.getId()).get();

GetResult jdkResult = couchbaseTemplate.getCouchbaseClientFactory().getDefaultCollection().get(st.getId());
assertNotEquals(0, st.getVersion());
assertEquals(jdkResult.cas(), st.getVersion());
}

@Test
@IgnoreWhen(clusterTypes = ClusterType.MOCKED)
Expand Down