Skip to content

Commit ef418d0

Browse files
committed
[B#48] MappingException when removing based on query using lt
1 parent 97c3da7 commit ef418d0

4 files changed

Lines changed: 45 additions & 10 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>3.2.0</version>
8+
<version>3.2.1</version>
99
<packaging>jar</packaging>
1010

1111
<name>relmongo</name>

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import io.github.kaiso.relmongo.mongo.DatabaseOperations;
2323

2424
import org.bson.Document;
25-
import org.bson.types.ObjectId;
2625
import org.springframework.data.mongodb.core.MongoOperations;
2726
import org.springframework.util.ReflectionUtils;
2827
import org.springframework.util.ReflectionUtils.FieldCallback;
@@ -40,14 +39,14 @@ public class PersistentPropertyCascadingRemoveCallback implements FieldCallback
4039

4140
private Document source;
4241
private MongoOperations mongoOperations;
43-
private Boolean loaded = Boolean.FALSE;
4442
private Object entity;
43+
private Class<?> entityType;
4544

46-
public PersistentPropertyCascadingRemoveCallback(Document source, MongoOperations mongoOperations, Class<?> clazz) {
45+
public PersistentPropertyCascadingRemoveCallback(Document source, MongoOperations mongoOperations, Class<?> entityType) {
4746
super();
4847
this.source = source;
4948
this.mongoOperations = mongoOperations;
50-
this.entity = mongoOperations.getConverter().read(clazz, source);
49+
this.entityType = entityType;
5150
}
5251

5352
public void doWith(Field field) throws IllegalAccessException {
@@ -64,7 +63,6 @@ private void doCascade(Field field, CascadeType cascadeType) throws IllegalAcces
6463
if (!Arrays.asList(CascadeType.REMOVE, CascadeType.ALL).contains(cascadeType)) {
6564
return;
6665
}
67-
loadEntity();
6866
Object child = field.get(entity);
6967
if (child != null) {
7068
if (Collection.class.isAssignableFrom(child.getClass())) {
@@ -87,21 +85,20 @@ private void cascadeCollection(Collection<?> child) {
8785
}
8886

8987
public void doProcessing() {
88+
loadEntity();
9089
if (entity == null) {
9190
return;
9291
}
9392
ReflectionUtils.doWithFields(entity.getClass(), this);
9493
}
9594

9695
private void loadEntity() {
97-
if (!loaded) {
96+
if (entity == null) {
9897
Object sourceId = this.source.get("_id");
9998
if (sourceId != null) {
100-
ObjectId id = sourceId instanceof ObjectId ? (ObjectId) sourceId : new ObjectId((String) sourceId);
101-
Collection<?> findByIds = DatabaseOperations.findByIds(mongoOperations, entity.getClass(), id);
99+
Collection<?> findByIds = DatabaseOperations.findByIds(mongoOperations, entityType, sourceId);
102100
this.entity = findByIds != null && !findByIds.isEmpty() ? findByIds.iterator().next() : null;
103101
}
104-
loaded = Boolean.TRUE;
105102
}
106103
}
107104

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public class Address {
2222

2323
@LastModifiedDate
2424
private LocalDateTime lastModifiedDate;
25+
26+
private LocalDateTime creationDate = LocalDateTime.now();
2527

2628
public ObjectId getId() {
2729
return id;
@@ -46,4 +48,9 @@ public LocalDateTime getLastModifiedDate() {
4648
public void setLastModifiedDate(LocalDateTime lastModifiedDate) {
4749
this.lastModifiedDate = lastModifiedDate;
4850
}
51+
52+
public LocalDateTime getCreationDate() {
53+
return creationDate;
54+
}
55+
4956
}

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
import org.bson.Document;
1010
import org.junit.jupiter.api.Test;
1111
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.data.mongodb.core.query.Criteria;
13+
import org.springframework.data.mongodb.core.query.Query;
1214

15+
import java.time.LocalDateTime;
1316
import java.util.Arrays;
1417
import java.util.LinkedList;
1518
import java.util.function.Consumer;
@@ -43,6 +46,32 @@ public void accept(Document t) {
4346
};
4447
mongoOperations.getCollection("addresses").find().forEach(f);
4548
}
49+
50+
@Test
51+
public void shouldRemoveByCriteria() {
52+
Address address1 = new Address();
53+
Address address2 = new Address();
54+
address1.setLocation("1st street");
55+
address2.setLocation("2st street");
56+
Person person = new Person();
57+
person.setName("Dave");
58+
person.setEmail("dave@mail.com");
59+
person.setAddresses(new LinkedList<Address>(Arrays.asList(address1, address2)));
60+
repository.save(person);
61+
Consumer<Document> f = new Consumer<Document>() {
62+
@Override
63+
public void accept(Document t) {
64+
assertNotNull(t.get("lastModifiedDate"));
65+
}
66+
};
67+
68+
69+
mongoOperations.getCollection("addresses").find().forEach(f);
70+
71+
Criteria expression = Criteria.where("creationDate").lt(LocalDateTime.now());
72+
Query query = new Query().addCriteria(expression);
73+
mongoOperations.remove(query, Address.class).wasAcknowledged();
74+
}
4675

4776
@Test
4877
public void shouldPopulateAuditingOnSave() {
@@ -57,5 +86,7 @@ public void accept(Document t) {
5786
};
5887
mongoOperations.getCollection("addresses").find().forEach(f);
5988
}
89+
90+
6091

6192
}

0 commit comments

Comments
 (0)