Skip to content

Commit 7612af4

Browse files
committed
fixed #101
1 parent 4ae1abb commit 7612af4

File tree

6 files changed

+50
-10
lines changed

6 files changed

+50
-10
lines changed

nitrite/src/main/java/org/dizitart/no2/util/ObjectUtils.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import static org.dizitart.no2.objects.filters.ObjectFilters.eq;
3939
import static org.dizitart.no2.util.ReflectionUtils.findAnnotations;
4040
import static org.dizitart.no2.util.ReflectionUtils.getField;
41+
import static org.dizitart.no2.util.ReflectionUtils.getFieldsUpto;
4142
import static org.dizitart.no2.util.StringUtils.isNullOrEmpty;
4243
import static org.dizitart.no2.util.ValidationUtils.notEmpty;
4344
import static org.dizitart.no2.util.ValidationUtils.notNull;
@@ -132,11 +133,11 @@ public static <T> Set<Index> extractIndices(NitriteMapper nitriteMapper, Class<T
132133
* @return the id field
133134
*/
134135
public static <T> Field getIdField(NitriteMapper nitriteMapper, Class<T> type) {
135-
Field[] fields;
136+
List<Field> fields;
136137
if (type.isAnnotationPresent(InheritIndices.class)) {
137-
fields = type.getFields();
138+
fields = getFieldsUpto(type, Object.class);
138139
} else {
139-
fields = type.getDeclaredFields();
140+
fields = Arrays.asList(type.getDeclaredFields());
140141
}
141142

142143
boolean alreadyIdFound = false;

nitrite/src/main/java/org/dizitart/no2/util/ReflectionUtils.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import lombok.experimental.UtilityClass;
2222
import org.dizitart.no2.exceptions.ValidationException;
23-
import org.dizitart.no2.objects.InheritIndices;
2423

2524
import java.lang.annotation.Annotation;
2625
import java.lang.reflect.Field;
@@ -58,7 +57,7 @@ public static List<Field> getFieldsUpto(Class<?> startClass, Class<?> exclusiveP
5857
filterSynthetics(currentClassFields);
5958
Class<?> parentClass = startClass.getSuperclass();
6059

61-
if (parentClass != null && (exclusiveParent == null || !(parentClass.equals(exclusiveParent)))) {
60+
if (parentClass != null && !(parentClass.equals(exclusiveParent))) {
6261
List<Field> parentClassFields = getFieldsUpto(parentClass, exclusiveParent);
6362
currentClassFields.addAll(parentClassFields);
6463
}
@@ -125,8 +124,9 @@ static <T> Field getField(Class<T> type, String name, boolean recursive) {
125124
break;
126125
}
127126
}
127+
128128
if (field == null && recursive) {
129-
Field[] fields = type.getFields();
129+
List<Field> fields = getFieldsUpto(type, Object.class);
130130
for (Field recursiveField : fields) {
131131
if (recursiveField.getName().equals(name)) {
132132
field = recursiveField;

nitrite/src/test/java/org/dizitart/no2/objects/ObjectRepositoryTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
import static org.dizitart.no2.DbTestOperations.getRandomTempDbFile;
3636
import static org.dizitart.no2.objects.filters.ObjectFilters.eq;
37+
import static org.dizitart.no2.objects.filters.ObjectFilters.text;
3738
import static org.junit.Assert.*;
3839

3940
/**
@@ -223,5 +224,31 @@ public void testWithIdInheritance() {
223224
assertTrue(repository.hasIndex("id"));
224225
assertTrue(repository.hasIndex("date"));
225226
assertTrue(repository.hasIndex("text"));
227+
228+
ChildClass childClass = new ChildClass();
229+
childClass.setName("first");
230+
childClass.setDate(new Date(100000L));
231+
childClass.setId(1L);
232+
childClass.setText("I am first class");
233+
repository.insert(childClass);
234+
235+
childClass = new ChildClass();
236+
childClass.setName("seconds");
237+
childClass.setDate(new Date(100001L));
238+
childClass.setId(2L);
239+
childClass.setText("I am second class");
240+
repository.insert(childClass);
241+
242+
childClass = new ChildClass();
243+
childClass.setName("third");
244+
childClass.setDate(new Date(100002L));
245+
childClass.setId(3L);
246+
childClass.setText("I am third class");
247+
repository.insert(childClass);
248+
249+
assertEquals(repository.find(text("text", "class")).size(), 3);
250+
assertEquals(repository.find(text("text", "second")).size(), 0); // filtered in stop words
251+
assertEquals(repository.find(eq("date", new Date(100000L))).size(), 1);
252+
assertEquals(repository.find(eq("id", 1L)).size(), 1);
226253
}
227254
}

nitrite/src/test/java/org/dizitart/no2/objects/data/ChildClass.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@
1818

1919
package org.dizitart.no2.objects.data;
2020

21+
import lombok.Getter;
22+
import lombok.Setter;
2123
import org.dizitart.no2.objects.InheritIndices;
2224

2325
/**
2426
* @author Anindya Chatterjee
2527
*/
28+
@Getter
29+
@Setter
2630
@InheritIndices
2731
public class ChildClass extends ParentClass {
28-
public String name;
32+
private String name;
2933
}

nitrite/src/test/java/org/dizitart/no2/objects/data/ParentClass.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
package org.dizitart.no2.objects.data;
2020

21+
import lombok.Getter;
22+
import lombok.Setter;
2123
import org.dizitart.no2.IndexType;
2224
import org.dizitart.no2.objects.Id;
2325
import org.dizitart.no2.objects.Index;
@@ -28,11 +30,13 @@
2830
/**
2931
* @author Anindya Chatterjee
3032
*/
33+
@Getter
34+
@Setter
3135
@Indices(
3236
@Index(value = "date", type = IndexType.Unique)
3337
)
3438
public class ParentClass extends SuperDuperClass {
3539
@Id
36-
public Long id;
37-
public Date date;
40+
protected Long id;
41+
private Date date;
3842
}

nitrite/src/test/java/org/dizitart/no2/objects/data/SuperDuperClass.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@
1818

1919
package org.dizitart.no2.objects.data;
2020

21+
import lombok.Getter;
22+
import lombok.Setter;
2123
import org.dizitart.no2.IndexType;
2224
import org.dizitart.no2.objects.Index;
2325

2426
/**
2527
* @author Anindya Chatterjee
2628
*/
29+
@Getter
30+
@Setter
2731
@Index(value = "text", type = IndexType.Fulltext)
2832
public class SuperDuperClass {
29-
public String text;
33+
private String text;
3034
}

0 commit comments

Comments
 (0)