Skip to content

Commit e53a813

Browse files
authored
Merge pull request #7 from kaiso/feature/#4
[FEATURE#4] add the collection attribute when storing the reference i…
2 parents 840b331 + 4b0a770 commit e53a813

4 files changed

Lines changed: 41 additions & 5 deletions

File tree

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
import io.github.kaiso.relmongo.annotation.JoinProperty;
2424
import io.github.kaiso.relmongo.annotation.OneToMany;
2525
import io.github.kaiso.relmongo.annotation.OneToOne;
26+
import io.github.kaiso.relmongo.util.ReflectionsUtil;
27+
import io.github.kaiso.relmongo.util.RelMongoConstants;
2628

29+
import org.springframework.data.mongodb.core.mapping.Document;
2730
import org.springframework.util.ReflectionUtils;
2831
import org.springframework.util.ReflectionUtils.FieldCallback;
2932

@@ -57,22 +60,27 @@ private void saveAssociation(Field field) {
5760
Object reference = null;
5861
try {
5962
reference = ((BasicDBObject) source).get(field.getName());
63+
String collection = getCollectionName(field);
6064
if (reference instanceof BasicDBList) {
6165
BasicDBList list = new BasicDBList();
62-
list.addAll(((BasicDBList) reference).stream().map(this::keepOnlyIdentifier).collect(Collectors.toList()));
66+
list.addAll(((BasicDBList) reference).stream().map(dbObject -> this.keepOnlyIdentifier(dbObject, collection)).collect(Collectors.toList()));
6367
((BasicDBObject) source).remove(field.getName());
6468
((BasicDBObject) source).put(name, list);
6569
} else if (reference instanceof BasicDBObject) {
6670
((BasicDBObject) source).remove(field.getName());
67-
((BasicDBObject) source).put(name, this.keepOnlyIdentifier(reference));
71+
((BasicDBObject) source).put(name, this.keepOnlyIdentifier(reference, collection));
6872
}
6973
} catch (Exception e) {
7074
throw new IllegalArgumentException("Property defined in @JoinProperty annotation is not present", e);
7175
}
7276
}
7377

74-
private BasicDBObject keepOnlyIdentifier(Object obj) {
75-
return new BasicDBObject().append("_id", ((DBObject) obj).get("_id"));
78+
private BasicDBObject keepOnlyIdentifier(Object obj, String collection) {
79+
return new BasicDBObject().append("_id", ((DBObject) obj).get("_id")).append(RelMongoConstants.RELMONGOTARGET_PROPERTY_NAME, collection);
80+
}
81+
82+
private String getCollectionName(Field field) {
83+
return ReflectionsUtil.getGenericType(field).getAnnotation(Document.class).collection();
7684
}
7785

7886
}

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import io.github.kaiso.relmongo.lazy.LazyLoadingProxy;
2525
import io.github.kaiso.relmongo.lazy.RelMongoLazyLoader;
2626
import io.github.kaiso.relmongo.model.LoadableObjectsMetadata;
27+
import io.github.kaiso.relmongo.util.RelMongoConstants;
2728

2829
import org.springframework.cglib.proxy.Enhancer;
2930
import org.springframework.cglib.proxy.Factory;
@@ -83,7 +84,21 @@ public static Object lazyLoader(Class<?> type, Object original, MongoOperations
8384
private static boolean hasToLoad(BasicDBList objects) {
8485
// the last contition verifies if the objects were already laoded by another
8586
// query
86-
return !objects.isEmpty() && ((BasicDBObject) objects.get(0)).size() <= 1;
87+
if (objects.isEmpty())
88+
return false;
89+
BasicDBObject basicDBObject = (BasicDBObject) objects.get(0);
90+
return hasToLoad(basicDBObject);
91+
}
92+
93+
/**
94+
* checks if an object is already populated to be loaded anther time or not
95+
*
96+
* @param basicDBObject
97+
* @return
98+
*/
99+
private static boolean hasToLoad(BasicDBObject basicDBObject) {
100+
int propscount = basicDBObject.get(RelMongoConstants.RELMONGOTARGET_PROPERTY_NAME) == null ? 1 : 2;
101+
return basicDBObject.size() <= propscount;
87102
}
88103

89104
public static Object mapIdentifier(Object object) {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.github.kaiso.relmongo.util;
2+
3+
public final class RelMongoConstants {
4+
5+
private RelMongoConstants() {
6+
super();
7+
}
8+
9+
public static final String RELMONGOTARGET_PROPERTY_NAME = "_relmongo_target";
10+
11+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import io.github.kaiso.relmongo.data.repository.PassportRepository;
1616
import io.github.kaiso.relmongo.data.repository.PersonRepository;
1717
import io.github.kaiso.relmongo.tests.common.AbstractBaseTest;
18+
import io.github.kaiso.relmongo.util.RelMongoConstants;
1819

1920
import org.junit.jupiter.api.BeforeEach;
2021
import org.junit.jupiter.api.Test;
@@ -104,6 +105,7 @@ public void shouldPersistOnlyReferencedPropertyOnOneToOneRelation() {
104105
System.out.println("relational id" + passport.getId());
105106
BasicDBObject obj = new BasicDBObject();
106107
obj.put("_id", passport.getId());
108+
obj.put(RelMongoConstants.RELMONGOTARGET_PROPERTY_NAME ,"passports");
107109
assertEquals(personNoRelational.get("passportref"), obj);
108110
}
109111

0 commit comments

Comments
 (0)