Skip to content

Commit a940729

Browse files
authored
Merge pull request #24 from kaiso/hotfix/#21
Hotfix/#21
2 parents a0a731c + c1ef933 commit a940729

6 files changed

Lines changed: 35 additions & 15 deletions

File tree

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>2.1.0</version>
8+
<version>2.1.1</version>
99
<packaging>jar</packaging>
1010

1111
<name>relmongo</name>

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
@@ -52,12 +52,13 @@ public PersistentPropertyLazyLoadingCallback(Object source, Document document, M
5252
public void doWith(Field field) throws IllegalAccessException {
5353
ReflectionUtils.makeAccessible(field);
5454

55-
if (DocumentUtils.isLoaded(document.get(field.getName()))) {
56-
return;
57-
}
58-
5955
if ((field.isAnnotationPresent(OneToMany.class) && FetchType.LAZY.equals(field.getAnnotation(OneToMany.class).fetch()))
6056
|| (field.isAnnotationPresent(OneToOne.class) && FetchType.LAZY.equals(field.getAnnotation(OneToOne.class).fetch()))) {
57+
58+
if (DocumentUtils.isLoaded(document.get(field.getName()))) {
59+
return;
60+
}
61+
6162
String joinPropertyName;
6263
try {
6364
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
@@ -69,7 +69,7 @@ private void loadAssociation(Field field, FetchType fetchType, Class<?> annotati
6969
try {
7070
ids = ((org.bson.Document) source).get(name);
7171

72-
if (DocumentUtils.isLoaded(((org.bson.Document) source).get(field.getName())) || ids == null
72+
if (ids == null || DocumentUtils.isLoaded(((org.bson.Document) source).get(field.getName()))
7373
|| (ids instanceof Document && ((Document) ids).keySet().isEmpty())) {
7474
return;
7575
}

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

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
*/
1616
package io.github.kaiso.relmongo.mongo;
1717

18+
import io.github.kaiso.relmongo.util.RelMongoConstants;
19+
20+
import org.bson.Document;
1821
import org.bson.types.ObjectId;
1922

2023
import java.util.Collection;
2124

2225
public final class DocumentUtils {
23-
24-
25-
2626

2727
private DocumentUtils() {
2828
super();
@@ -35,10 +35,30 @@ private DocumentUtils() {
3535
* @return
3636
*/
3737
public static boolean isLoaded(Object obj) {
38-
return (obj != null && Collection.class.isAssignableFrom(obj.getClass()) && !((Collection<?>) obj).isEmpty()) || (obj != null);
38+
if (obj != null) {
39+
if (Collection.class.isAssignableFrom(obj.getClass()) && !((Collection<?>) obj).isEmpty()) {
40+
Object document = ((Collection<?>) obj).iterator().next();
41+
if (document instanceof Document) {
42+
return isDocumentLoaded((Document) document);
43+
}
44+
} else if (obj instanceof Document) {
45+
return isDocumentLoaded((Document) obj);
46+
}
47+
}
48+
return false;
49+
}
50+
51+
private static boolean isDocumentLoaded(Document dbObject) {
52+
int counter = 0;
53+
if (dbObject.containsKey("_id")) {
54+
counter++;
55+
}
56+
if (dbObject.containsKey(RelMongoConstants.RELMONGOTARGET_PROPERTY_NAME)) {
57+
counter++;
58+
}
59+
return dbObject.keySet().size() > counter;
3960
}
40-
41-
61+
4262
public static ObjectId mapIdentifier(Object object) {
4363
return ((org.bson.Document) object).getObjectId("_id");
4464
}

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
@@ -26,7 +26,7 @@ public class Person {
2626
private List<House> houses;
2727

2828
@OneToOne(fetch=FetchType.EAGER)
29-
@JoinProperty(name = "passportref")
29+
@JoinProperty(name = "passport")
3030
private Passport passport;
3131

3232
@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
@@ -111,13 +111,12 @@ public void shouldPersistOnlyReferencedPropertyOnOneToOneRelation() {
111111
person.setPassport(passport);
112112
repository.save(person);
113113
Document document = mongoOperations.getCollection("people").find().iterator().next();
114-
assertNull(document.get("passport"));
115114
System.out.println("db id" + document.get("passportId"));
116115
System.out.println("relational id" + passport.getId());
117116
Document obj = new Document();
118117
obj.put("_id", passport.getId());
119118
obj.put(RelMongoConstants.RELMONGOTARGET_PROPERTY_NAME, "passport");
120-
assertEquals(document.get("passportref"), obj);
119+
assertEquals(document.get("passport"), obj);
121120
}
122121

123122
@Test

0 commit comments

Comments
 (0)