DATACOUCH-145 - Use Id/Field annotations from Couchbase SDK
The @Field annotation has been removed in favor of the one from the Couchbase SDK. Similarly, CouchbasePersistentEntity now recognize the SDK's @Id annotation. Note that if both Spring Data and Couchbase @Id are present (on two distinct fields), the one from Spring Data will be taken into account.
This commit is contained in:
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.data.couchbase.core.mapping;
|
||||
|
||||
import com.couchbase.client.java.repository.annotation.Id;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
@@ -61,6 +63,38 @@ public class BasicCouchbasePersistentEntity<T> extends BasicPersistentEntity<T,
|
||||
context.setRootObject(applicationContext);
|
||||
}
|
||||
|
||||
// DATACOUCH-145: allows SDK's @Id annotation to be used
|
||||
@Override
|
||||
protected CouchbasePersistentProperty returnPropertyIfBetterIdPropertyCandidateOrNull(CouchbasePersistentProperty property) {
|
||||
if (!property.isIdProperty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!this.hasIdProperty()) {
|
||||
return property;
|
||||
}
|
||||
|
||||
//check existing ID vs new candidate
|
||||
boolean currentCbId = this.getIdProperty().isAnnotationPresent(Id.class);
|
||||
boolean currentSpringId = this.getIdProperty().isAnnotationPresent(org.springframework.data.annotation.Id.class);
|
||||
boolean candidateCbId = property.isAnnotationPresent(Id.class);
|
||||
boolean candidateSpringId = property.isAnnotationPresent(org.springframework.data.annotation.Id.class);
|
||||
|
||||
if (currentCbId && candidateSpringId) {
|
||||
//spring IDs will have priority over SDK IDs
|
||||
return property;
|
||||
} else if (currentSpringId && candidateCbId) {
|
||||
//ignore SDK's IDs if current is a Spring ID
|
||||
return null;
|
||||
}
|
||||
/* any of the following will throw:
|
||||
- current is a spring ID and the candidate bears another spring ID
|
||||
- current is a SDK ID and the candidate bears another SDK ID
|
||||
- any other combination involving something else than a SDK or Spring ID
|
||||
*/
|
||||
return super.returnPropertyIfBetterIdPropertyCandidateOrNull(property);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the expiration time of the entity.
|
||||
*
|
||||
|
||||
@@ -19,6 +19,8 @@ package org.springframework.data.couchbase.core.mapping;
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import com.couchbase.client.java.repository.annotation.Id;
|
||||
|
||||
import org.springframework.data.mapping.Association;
|
||||
import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty;
|
||||
import org.springframework.data.mapping.model.FieldNamingStrategy;
|
||||
@@ -28,7 +30,7 @@ import org.springframework.data.mapping.model.SimpleTypeHolder;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Implements annotated property representations of a given Field instance.
|
||||
* Implements annotated property representations of a given {@link com.couchbase.client.java.repository.annotation.Field} instance.
|
||||
* <p/>
|
||||
* <p>This object is used to gather information out of properties on objects that need to be persisted. For example, it
|
||||
* supports overriding of the actual property name by providing custom annotations.</p>
|
||||
@@ -73,8 +75,8 @@ public class BasicCouchbasePersistentProperty
|
||||
*/
|
||||
@Override
|
||||
public String getFieldName() {
|
||||
org.springframework.data.couchbase.core.mapping.Field annotation = getField().
|
||||
getAnnotation(org.springframework.data.couchbase.core.mapping.Field.class);
|
||||
com.couchbase.client.java.repository.annotation.Field annotation = getField().
|
||||
getAnnotation(com.couchbase.client.java.repository.annotation.Field.class);
|
||||
|
||||
if (annotation != null && StringUtils.hasText(annotation.value())) {
|
||||
return annotation.value();
|
||||
@@ -90,4 +92,9 @@ public class BasicCouchbasePersistentProperty
|
||||
return fieldName;
|
||||
}
|
||||
|
||||
// DATACOUCH-145: allows SDK's @Id annotation to be used
|
||||
@Override
|
||||
public boolean isIdProperty() {
|
||||
return isAnnotationPresent(Id.class) || super.isIdProperty();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2015 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.core.mapping;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
/**
|
||||
* Annotation to define custom metadata for document fields.
|
||||
*
|
||||
* @author Michael Nitschinger
|
||||
*/
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Field {
|
||||
|
||||
/**
|
||||
* The key to be used to store the field inside the document.
|
||||
*/
|
||||
String value() default "";
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user