Field(name=myName) annotation work like Field(myName) and Field(value=myName). (#1186)
Closes #1068.
This commit is contained in:
@@ -45,6 +45,7 @@ public class BasicCouchbasePersistentProperty extends AnnotationBasedPersistentP
|
||||
implements CouchbasePersistentProperty {
|
||||
|
||||
private final FieldNamingStrategy fieldNamingStrategy;
|
||||
private String fieldName;
|
||||
|
||||
/**
|
||||
* Create a new instance of the BasicCouchbasePersistentProperty class.
|
||||
@@ -75,25 +76,32 @@ public class BasicCouchbasePersistentProperty extends AnnotationBasedPersistentP
|
||||
*/
|
||||
@Override
|
||||
public String getFieldName() {
|
||||
if (fieldName != null) {
|
||||
return fieldName;
|
||||
}
|
||||
Field annotationField = getField().getAnnotation(Field.class);
|
||||
|
||||
if (annotationField != null && StringUtils.hasText(annotationField.value())) {
|
||||
return annotationField.value();
|
||||
if (annotationField != null) {
|
||||
if (StringUtils.hasText(annotationField.value())) {
|
||||
return fieldName = annotationField.value();
|
||||
} else if (StringUtils.hasText(annotationField.name())) {
|
||||
return fieldName = annotationField.name();
|
||||
}
|
||||
}
|
||||
JsonProperty annotation = getField().getAnnotation(JsonProperty.class);
|
||||
|
||||
if (annotation != null && StringUtils.hasText(annotation.value())) {
|
||||
return annotation.value();
|
||||
return fieldName = annotation.value();
|
||||
}
|
||||
|
||||
String fieldName = fieldNamingStrategy.getFieldName(this);
|
||||
String fName = fieldNamingStrategy.getFieldName(this);
|
||||
|
||||
if (!StringUtils.hasText(fieldName)) {
|
||||
if (!StringUtils.hasText(fName)) {
|
||||
throw new MappingException(String.format("Invalid (null or empty) field name returned for property %s by %s!",
|
||||
this, fieldNamingStrategy.getClass()));
|
||||
}
|
||||
|
||||
return fieldName;
|
||||
return fieldName = fName;
|
||||
}
|
||||
|
||||
// DATACOUCH-145: allows SDK's @Id annotation to be used
|
||||
|
||||
@@ -43,6 +43,7 @@ public class Person extends AbstractEntity {
|
||||
@Version private long version;
|
||||
|
||||
@Nullable @Field("nickname") private String middlename;
|
||||
@Nullable @Field(name = "prefix") private String salutation;
|
||||
|
||||
private Address address;
|
||||
|
||||
@@ -99,10 +100,18 @@ public class Person extends AbstractEntity {
|
||||
return middlename;
|
||||
}
|
||||
|
||||
public String getSalutation() {
|
||||
return salutation;
|
||||
}
|
||||
|
||||
public void setMiddlename(String middlename) {
|
||||
this.middlename = middlename;
|
||||
}
|
||||
|
||||
public void setSalutation(String salutation) {
|
||||
this.salutation = salutation;
|
||||
}
|
||||
|
||||
public long getVersion() {
|
||||
return version;
|
||||
}
|
||||
@@ -143,15 +152,4 @@ public class Person extends AbstractEntity {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
static String optional(String name, String obj) {
|
||||
if (obj != null) {
|
||||
if (obj != null /*.isPresent() */) {
|
||||
return (" " + name + ": '" + obj/*.get()*/ + "'\n");
|
||||
} else {
|
||||
return " " + name + ": null\n";
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -112,4 +112,7 @@ public interface PersonRepository extends CrudRepository<Person, String> {
|
||||
|
||||
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
|
||||
List<Person> findByMiddlename(String nickName);
|
||||
|
||||
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
|
||||
List<Person> findBySalutation(String prefix);
|
||||
}
|
||||
|
||||
@@ -32,11 +32,11 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.couchbase.client.java.kv.UpsertOptions;
|
||||
@@ -85,6 +85,7 @@ import com.couchbase.client.core.error.IndexExistsException;
|
||||
import com.couchbase.client.core.error.IndexFailureException;
|
||||
import com.couchbase.client.java.env.ClusterEnvironment;
|
||||
import com.couchbase.client.java.json.JsonArray;
|
||||
import com.couchbase.client.java.kv.GetResult;
|
||||
import com.couchbase.client.java.kv.MutationState;
|
||||
import com.couchbase.client.java.query.QueryOptions;
|
||||
import com.couchbase.client.java.query.QueryScanConsistency;
|
||||
@@ -169,6 +170,27 @@ public class CouchbaseRepositoryQueryIntegrationTests extends ClusterAwareIntegr
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void annotatedFieldFindName() {
|
||||
Person person = null;
|
||||
try {
|
||||
person = new Person(1, "first", "last");
|
||||
person.setSalutation("Mrs"); // salutation is stored as prefix
|
||||
personRepository.save(person);
|
||||
GetResult result = couchbaseTemplate.getCouchbaseClientFactory().getBucket().defaultCollection()
|
||||
.get(person.getId().toString());
|
||||
assertEquals(person.getSalutation(), result.contentAsObject().get("prefix"));
|
||||
Person person2 = personRepository.findById(person.getId().toString()).get();
|
||||
assertEquals(person.getSalutation(), person2.getSalutation());
|
||||
// needs fix from datacouch_1184
|
||||
//List<Person> persons3 = personRepository.findBySalutation("Mrs");
|
||||
//assertEquals(1, persons3.size());
|
||||
//assertEquals(person.getSalutation(), persons3.get(0).getSalutation());
|
||||
} finally {
|
||||
personRepository.deleteById(person.getId().toString());
|
||||
}
|
||||
}
|
||||
|
||||
// "1\" or name=name or name=\"1")
|
||||
@Test
|
||||
void findByInjection() {
|
||||
@@ -395,7 +417,7 @@ public class CouchbaseRepositoryQueryIntegrationTests extends ClusterAwareIntegr
|
||||
userRepository.save(user1);
|
||||
userRepository.save(user2);
|
||||
List<User> users = userRepository.findByLastname("Wilson").collect(Collectors.toList());
|
||||
assertEquals(2,users.size());
|
||||
assertEquals(2, users.size());
|
||||
assertTrue(users.contains(user1));
|
||||
assertTrue(users.contains(user2));
|
||||
userRepository.delete(user1);
|
||||
|
||||
Reference in New Issue
Block a user