Skip to content

Commit 7a7aa23

Browse files
authored
Merge pull request #22 from kaiso/support/hotfix/#21
[bug][#21] child document is not loaded when the joinproperty and the…
2 parents 7fd62e4 + 1c6aa53 commit 7a7aa23

5 files changed

Lines changed: 31 additions & 14 deletions

File tree

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,13 @@ public PersistentPropertyLazyLoadingCallback(Object source, DBObject document, M
5353
public void doWith(Field field) throws IllegalAccessException {
5454
ReflectionUtils.makeAccessible(field);
5555

56-
if (DocumentUtils.isLoaded(document.get(field.getName()))) {
57-
return;
58-
}
59-
6056
if ((field.isAnnotationPresent(OneToMany.class) && FetchType.LAZY.equals(field.getAnnotation(OneToMany.class).fetch()))
6157
|| (field.isAnnotationPresent(OneToOne.class) && FetchType.LAZY.equals(field.getAnnotation(OneToOne.class).fetch()))) {
58+
59+
if (DocumentUtils.isLoaded(document.get(field.getName()))) {
60+
return;
61+
}
62+
6263
String joinPropertyName;
6364
try {
6465
joinPropertyName = field.getAnnotation(JoinProperty.class).name();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ private void loadAssociation(Field field, FetchType fetchType, Class<?> annotati
7171

7272
ids = ((BasicDBObject) source).get(name);
7373

74-
if (DocumentUtils.isLoaded(((BasicDBObject) source).get(field.getName())) || ids == null || ((DBObject)ids).keySet().isEmpty()) {
74+
if (ids == null || DocumentUtils.isLoaded(((BasicDBObject) source).get(field.getName())) || ((DBObject)ids).keySet().isEmpty()) {
7575
return;
7676
}
7777

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

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616
package io.github.kaiso.relmongo.mongo;
1717

1818
import com.mongodb.BasicDBObject;
19+
import com.mongodb.DBObject;
20+
21+
import io.github.kaiso.relmongo.util.RelMongoConstants;
1922

2023
import org.bson.types.ObjectId;
2124

2225
import java.util.Collection;
2326

2427
public final class DocumentUtils {
25-
26-
27-
2828

2929
private DocumentUtils() {
3030
super();
@@ -37,10 +37,27 @@ private DocumentUtils() {
3737
* @return
3838
*/
3939
public static boolean isLoaded(Object obj) {
40-
return (obj != null && Collection.class.isAssignableFrom(obj.getClass()) && !((Collection<?>) obj).isEmpty()) || (obj != null);
40+
if (obj != null) {
41+
if (Collection.class.isAssignableFrom(obj.getClass()) && !((Collection<?>) obj).isEmpty()) {
42+
return isDBObjectLoaded((DBObject) ((Collection<?>) obj).iterator().next());
43+
} else {
44+
return isDBObjectLoaded((DBObject) obj);
45+
}
46+
}
47+
return false;
48+
}
49+
50+
private static boolean isDBObjectLoaded(DBObject dbObject) {
51+
int counter = 0;
52+
if (dbObject.containsField("_id")) {
53+
counter++;
54+
}
55+
if (dbObject.containsField(RelMongoConstants.RELMONGOTARGET_PROPERTY_NAME)) {
56+
counter++;
57+
}
58+
return dbObject.keySet().size() > counter;
4159
}
42-
43-
60+
4461
public static ObjectId mapIdentifier(Object object) {
4562
return ((BasicDBObject) object).getObjectId("_id");
4663
}

src/test/java/io/github/kaiso/relmongo/data/model/Person.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class Person {
2727
private List<House> houses = new ArrayList<>();
2828

2929
@OneToOne(fetch = FetchType.EAGER)
30-
@JoinProperty(name = "passportref")
30+
@JoinProperty(name = "passport")
3131
private Passport passport;
3232

3333
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)

src/test/java/io/github/kaiso/relmongo/tests/PersonRepositoryTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,12 @@ public void shouldPersistOnlyReferencedPropertyOnOneToOneRelation() {
114114
person.setPassport(passport);
115115
repository.save(person);
116116
DBObject personNoRelational = mongoOperations.getCollection("people").find().next();
117-
assertNull(personNoRelational.get("passport"));
118117
System.out.println("db id" + personNoRelational.get("passportId"));
119118
System.out.println("relational id" + passport.getId());
120119
BasicDBObject obj = new BasicDBObject();
121120
obj.put("_id", passport.getId());
122121
obj.put(RelMongoConstants.RELMONGOTARGET_PROPERTY_NAME, "passport");
123-
assertEquals(personNoRelational.get("passportref"), obj);
122+
assertEquals(personNoRelational.get("passport"), obj);
124123
}
125124

126125
@Test

0 commit comments

Comments
 (0)