Skip to content

Commit 1b09bd1

Browse files
authored
Merge pull request #9 from kaiso/feature/#8
Feature/#8
2 parents 3359a43 + 0d2c1a8 commit 1b09bd1

4 files changed

Lines changed: 13 additions & 5 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
target/
22
.classpath
33
.project
4+
pom.xml.versionsBackup

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>io.github.kaiso.relmongo</groupId>
77
<artifactId>relmongo</artifactId>
8-
<version>1.0.0-beta</version>
8+
<version>1.0.0-RC2</version>
99
<packaging>jar</packaging>
1010

1111
<name>relmongo</name>

src/main/java/io/github/kaiso/relmongo/events/callback/PersistentPropertyLoadingCallback.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package io.github.kaiso.relmongo.events.callback;
1818

19+
import com.mongodb.BasicDBList;
1920
import com.mongodb.BasicDBObject;
2021

2122
import io.github.kaiso.relmongo.annotation.FetchType;
@@ -47,15 +48,15 @@ public void doWith(Field field) throws IllegalAccessException {
4748
FetchType fetchType = null;
4849
if (field.isAnnotationPresent(OneToMany.class)) {
4950
fetchType = field.getAnnotation(OneToMany.class).fetch();
50-
loadAssociation(field, fetchType);
51+
loadAssociation(field, fetchType, OneToMany.class);
5152
} else if (field.isAnnotationPresent(OneToOne.class)) {
5253
fetchType = field.getAnnotation(OneToOne.class).fetch();
53-
loadAssociation(field, fetchType);
54+
loadAssociation(field, fetchType, OneToOne.class);
5455
}
5556

5657
}
5758

58-
private void loadAssociation(Field field, FetchType fetchType) {
59+
private void loadAssociation(Field field, FetchType fetchType, Class<?> annotation) {
5960
String name = "";
6061
try {
6162
name = field.getAnnotation(JoinProperty.class).name();
@@ -65,6 +66,12 @@ private void loadAssociation(Field field, FetchType fetchType) {
6566
Object ids = null;
6667
try {
6768
ids = ((BasicDBObject) source).get(name);
69+
if (OneToOne.class.equals(annotation) && ids instanceof BasicDBList) {
70+
// mongo database lookups return always an array if unwind is not used event if
71+
// it is a one ot one association
72+
BasicDBList list = (BasicDBList) ids;
73+
ids = list.isEmpty() ? list : list.get(0);
74+
}
6875
} catch (Exception e) {
6976
throw new IllegalArgumentException("Property defined in @JoinProperty annotation is not present", e);
7077
}

src/main/java/io/github/kaiso/relmongo/mongo/PersistentRelationResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static void resolveOnLoading(MongoOperations mongoOperations, List<Loadab
5555
} else {
5656
source.put(relation.getFieldName(), relation.getObjectIds());
5757
}
58-
} else if (relation.getObjectIds() instanceof BasicDBObject) {
58+
} else if (relation.getObjectIds() instanceof BasicDBObject && hasToLoad((BasicDBObject) relation.getObjectIds())) {
5959
if (FetchType.EAGER.equals(relation.getFetchType())) {
6060
source.put(relation.getFieldName(), DatabaseLoader.getDocumentByPropertyValue(mongoOperations, mapIdentifier(relation.getObjectIds()),
6161
relation.getReferencedPropertyName(), collection));

0 commit comments

Comments
 (0)