Skip to content

Commit c414e2c

Browse files
committed
[BUG#11] Exception encountered upon retrieval of relmongo annotated entity from repository
1 parent 5e768fb commit c414e2c

6 files changed

Lines changed: 35 additions & 20 deletions

File tree

.gitignore

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

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.0.0-RC1</version>
8+
<version>2.0.0-RC2</version>
99
<packaging>jar</packaging>
1010

1111
<name>relmongo</name>

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ private org.bson.Document keepOnlyIdentifier(Object obj, String collection) {
7878
}
7979

8080
private String getCollectionName(Field field) {
81-
return ReflectionsUtil.getGenericType(field).getAnnotation(Document.class).collection();
81+
String collection = ReflectionsUtil.getGenericType(field).getAnnotation(Document.class).collection();
82+
if(collection == null || "".equals(collection)) {
83+
collection = ReflectionsUtil.getGenericType(field).getSimpleName().toLowerCase();
84+
}
85+
return collection;
8286
}
8387

8488
}

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,9 @@
1616

1717
package io.github.kaiso.relmongo.mongo;
1818

19-
import com.mongodb.BasicDBList;
20-
import com.mongodb.BasicDBObject;
21-
22-
import io.github.kaiso.relmongo.annotation.FetchType;
23-
import io.github.kaiso.relmongo.lazy.LazyLoadingProxy;
24-
import io.github.kaiso.relmongo.lazy.RelMongoLazyLoader;
25-
import io.github.kaiso.relmongo.model.LoadableObjectsMetadata;
26-
import io.github.kaiso.relmongo.util.RelMongoConstants;
19+
import java.util.Collection;
20+
import java.util.List;
21+
import java.util.stream.Collectors;
2722

2823
import org.springframework.cglib.proxy.Enhancer;
2924
import org.springframework.cglib.proxy.Factory;
@@ -32,9 +27,11 @@
3227
import org.springframework.data.mongodb.core.mapping.Document;
3328
import org.springframework.objenesis.ObjenesisStd;
3429

35-
import java.util.Collection;
36-
import java.util.List;
37-
import java.util.stream.Collectors;
30+
import io.github.kaiso.relmongo.annotation.FetchType;
31+
import io.github.kaiso.relmongo.lazy.LazyLoadingProxy;
32+
import io.github.kaiso.relmongo.lazy.RelMongoLazyLoader;
33+
import io.github.kaiso.relmongo.model.LoadableObjectsMetadata;
34+
import io.github.kaiso.relmongo.util.RelMongoConstants;
3835

3936
public final class PersistentRelationResolver {
4037

@@ -47,6 +44,9 @@ private PersistentRelationResolver() {
4744
public static void resolveOnLoading(MongoOperations mongoOperations, List<LoadableObjectsMetadata> loadableObjects, org.bson.Document document) {
4845
for (LoadableObjectsMetadata relation : loadableObjects) {
4946
String collection = relation.getTargetAssociationClass().getAnnotation(Document.class).collection();
47+
if(collection == null || "".equals(collection)) {
48+
collection = relation.getTargetAssociationClass().getSimpleName().toLowerCase();
49+
}
5050
if (relation.getObjectIds() instanceof Collection && hasToLoad((Collection<?>) relation.getObjectIds())) {
5151
if (FetchType.EAGER.equals(relation.getFetchType())) {
5252
List<Object> identifierList = ((Collection<?>) relation.getObjectIds()).stream().map(PersistentRelationResolver::mapIdentifier)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import org.springframework.data.annotation.Id;
55
import org.springframework.data.mongodb.core.mapping.Document;
66

7-
@Document(collection = "passports")
7+
@Document()
88
public class Passport {
99

1010
@Id

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.test.context.ContextConfiguration;
2323

2424
import java.util.Arrays;
25+
import java.util.Collection;
2526
import java.util.Optional;
2627

2728
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -74,18 +75,27 @@ public void shouldSaveAndRetreive() {
7475

7576
@Test
7677
public void shouldPersistOnlyIdOnOneToManyRelation() {
77-
Car car = new Car();
78-
car.setColor(Color.BLUE);
79-
car.setManufacturer("BMW");
80-
carRepository.save(car);
78+
Car car1 = new Car();
79+
car1.setColor(Color.BLUE);
80+
car1.setManufacturer("BMW");
81+
Car car2 = new Car();
82+
car2.setColor(Color.BLUE);
83+
car2.setManufacturer("BMW");
84+
carRepository.save(car1);
85+
carRepository.save(car2);
8186
Person person = new Person();
8287
person.setName("Dave");
8388
person.setEmail("dave@mail.com");
84-
person.setCars(Arrays.asList(new Car[] { car }));
89+
person.setCars(Arrays.asList(new Car[] { car1, car2 }));
8590
repository.save(person);
8691

8792
Document document = mongoOperations.getCollection("people").find().iterator().next();
8893
assertNull(document.get("cars"));
94+
Document obj = new Document();
95+
obj.put("_id", car1.getId());
96+
obj.put(RelMongoConstants.RELMONGOTARGET_PROPERTY_NAME, "cars");
97+
assertTrue(((Collection<?>)document.get("carsrefs")).size() == 2);
98+
assertEquals(((Collection<?>)document.get("carsrefs")).iterator().next(), obj);
8999
}
90100

91101
@Test
@@ -104,7 +114,7 @@ public void shouldPersistOnlyReferencedPropertyOnOneToOneRelation() {
104114
System.out.println("relational id" + passport.getId());
105115
Document obj = new Document();
106116
obj.put("_id", passport.getId());
107-
obj.put(RelMongoConstants.RELMONGOTARGET_PROPERTY_NAME, "passports");
117+
obj.put(RelMongoConstants.RELMONGOTARGET_PROPERTY_NAME, "passport");
108118
assertEquals(document.get("passportref"), obj);
109119
}
110120

0 commit comments

Comments
 (0)