DATACOUCH-533 - Promote id field if no annotation id.

Promote id field if no annotation id.
Note that there is no longer any Couchbase @Id annotation - only
the Spring @id annotation.
This commit is contained in:
mikereiche
2020-06-30 18:06:29 -07:00
parent ecaf58db75
commit d2456bb166
4 changed files with 209 additions and 13 deletions

View File

@@ -23,6 +23,8 @@ import java.util.concurrent.TimeUnit;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.data.annotation.Id;
import org.springframework.data.mapping.MappingException;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.model.BasicPersistentEntity;
import org.springframework.data.util.TypeInformation;
import org.springframework.util.Assert;
@@ -76,24 +78,22 @@ public class BasicCouchbasePersistentEntity<T> extends BasicPersistentEntity<T,
}
// 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
if (candidateSpringId && !currentSpringId) {
// spring IDs will have priority over fields named id
return property;
} else if (currentSpringId && candidateCbId) {
// ignore SDK's IDs if current is a Spring ID
} else if (currentSpringId && !candidateSpringId) {
// spring IDs will have priority over fields named id
return null;
} else {
// do not allow two @Id fields or two fields named id (possible via @Field)
throw new MappingException(String.format(
"Attempt to add id property %s but already have property %s registered as id. Check your mapping configuration!",
property.getField(), getIdProperty().getField()));
}
/* 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);
}
@Override

View File

@@ -28,6 +28,8 @@ import org.springframework.util.StringUtils;
import com.couchbase.client.core.deps.com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Locale;
/**
* Implements annotated property representations of a given {@link Field} instance.
* <p/>
@@ -97,6 +99,7 @@ public class BasicCouchbasePersistentProperty extends AnnotationBasedPersistentP
// DATACOUCH-145: allows SDK's @Id annotation to be used
@Override
public boolean isIdProperty() {
return isAnnotationPresent(Id.class) || super.isIdProperty();
return isAnnotationPresent(Id.class) || super.isIdProperty()
|| this.getFieldName().toLowerCase(Locale.ROOT).equals("id");
}
}