Skip to content

Commit d210678

Browse files
authored
Field(name=myName) annotation work like Field(myName) and Field(value=myName). (#1186)
Closes #1068.
1 parent 52b1832 commit d210678

File tree

4 files changed

+50
-19
lines changed

4 files changed

+50
-19
lines changed

src/main/java/org/springframework/data/couchbase/core/mapping/BasicCouchbasePersistentProperty.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class BasicCouchbasePersistentProperty extends AnnotationBasedPersistentP
4545
implements CouchbasePersistentProperty {
4646

4747
private final FieldNamingStrategy fieldNamingStrategy;
48+
private String fieldName;
4849

4950
/**
5051
* Create a new instance of the BasicCouchbasePersistentProperty class.
@@ -75,25 +76,32 @@ protected Association<CouchbasePersistentProperty> createAssociation() {
7576
*/
7677
@Override
7778
public String getFieldName() {
79+
if (fieldName != null) {
80+
return fieldName;
81+
}
7882
Field annotationField = getField().getAnnotation(Field.class);
7983

80-
if (annotationField != null && StringUtils.hasText(annotationField.value())) {
81-
return annotationField.value();
84+
if (annotationField != null) {
85+
if (StringUtils.hasText(annotationField.value())) {
86+
return fieldName = annotationField.value();
87+
} else if (StringUtils.hasText(annotationField.name())) {
88+
return fieldName = annotationField.name();
89+
}
8290
}
8391
JsonProperty annotation = getField().getAnnotation(JsonProperty.class);
8492

8593
if (annotation != null && StringUtils.hasText(annotation.value())) {
86-
return annotation.value();
94+
return fieldName = annotation.value();
8795
}
8896

89-
String fieldName = fieldNamingStrategy.getFieldName(this);
97+
String fName = fieldNamingStrategy.getFieldName(this);
9098

91-
if (!StringUtils.hasText(fieldName)) {
99+
if (!StringUtils.hasText(fName)) {
92100
throw new MappingException(String.format("Invalid (null or empty) field name returned for property %s by %s!",
93101
this, fieldNamingStrategy.getClass()));
94102
}
95103

96-
return fieldName;
104+
return fieldName = fName;
97105
}
98106

99107
// DATACOUCH-145: allows SDK's @Id annotation to be used

src/test/java/org/springframework/data/couchbase/domain/Person.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public class Person extends AbstractEntity {
4343
@Version private long version;
4444

4545
@Nullable @Field("nickname") private String middlename;
46+
@Nullable @Field(name = "prefix") private String salutation;
4647

4748
private Address address;
4849

@@ -99,10 +100,18 @@ public String getMiddlename() {
99100
return middlename;
100101
}
101102

103+
public String getSalutation() {
104+
return salutation;
105+
}
106+
102107
public void setMiddlename(String middlename) {
103108
this.middlename = middlename;
104109
}
105110

111+
public void setSalutation(String salutation) {
112+
this.salutation = salutation;
113+
}
114+
106115
public long getVersion() {
107116
return version;
108117
}
@@ -143,15 +152,4 @@ public String toString() {
143152
return sb.toString();
144153
}
145154

146-
static String optional(String name, String obj) {
147-
if (obj != null) {
148-
if (obj != null /*.isPresent() */) {
149-
return (" " + name + ": '" + obj/*.get()*/ + "'\n");
150-
} else {
151-
return " " + name + ": null\n";
152-
}
153-
}
154-
return "";
155-
}
156-
157155
}

src/test/java/org/springframework/data/couchbase/domain/PersonRepository.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,7 @@ public interface PersonRepository extends CrudRepository<Person, String> {
112112

113113
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
114114
List<Person> findByMiddlename(String nickName);
115+
116+
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
117+
List<Person> findBySalutation(String prefix);
115118
}

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@
3232
import java.util.Arrays;
3333
import java.util.List;
3434
import java.util.Locale;
35+
import java.util.Optional;
3536
import java.util.concurrent.Callable;
3637
import java.util.concurrent.ExecutorService;
3738
import java.util.concurrent.Executors;
3839
import java.util.concurrent.Future;
39-
import java.util.Optional;
4040
import java.util.stream.Collectors;
4141

4242
import com.couchbase.client.java.kv.UpsertOptions;
@@ -85,6 +85,7 @@
8585
import com.couchbase.client.core.error.IndexFailureException;
8686
import com.couchbase.client.java.env.ClusterEnvironment;
8787
import com.couchbase.client.java.json.JsonArray;
88+
import com.couchbase.client.java.kv.GetResult;
8889
import com.couchbase.client.java.kv.MutationState;
8990
import com.couchbase.client.java.query.QueryOptions;
9091
import com.couchbase.client.java.query.QueryScanConsistency;
@@ -169,6 +170,27 @@ void annotatedFieldFind() {
169170
}
170171
}
171172

173+
@Test
174+
void annotatedFieldFindName() {
175+
Person person = null;
176+
try {
177+
person = new Person(1, "first", "last");
178+
person.setSalutation("Mrs"); // salutation is stored as prefix
179+
personRepository.save(person);
180+
GetResult result = couchbaseTemplate.getCouchbaseClientFactory().getBucket().defaultCollection()
181+
.get(person.getId().toString());
182+
assertEquals(person.getSalutation(), result.contentAsObject().get("prefix"));
183+
Person person2 = personRepository.findById(person.getId().toString()).get();
184+
assertEquals(person.getSalutation(), person2.getSalutation());
185+
// needs fix from datacouch_1184
186+
//List<Person> persons3 = personRepository.findBySalutation("Mrs");
187+
//assertEquals(1, persons3.size());
188+
//assertEquals(person.getSalutation(), persons3.get(0).getSalutation());
189+
} finally {
190+
personRepository.deleteById(person.getId().toString());
191+
}
192+
}
193+
172194
// "1\" or name=name or name=\"1")
173195
@Test
174196
void findByInjection() {
@@ -395,7 +417,7 @@ public void testStreamQuery() {
395417
userRepository.save(user1);
396418
userRepository.save(user2);
397419
List<User> users = userRepository.findByLastname("Wilson").collect(Collectors.toList());
398-
assertEquals(2,users.size());
420+
assertEquals(2, users.size());
399421
assertTrue(users.contains(user1));
400422
assertTrue(users.contains(user2));
401423
userRepository.delete(user1);

0 commit comments

Comments
 (0)