Skip to content

Commit 5de3cb9

Browse files
laszlocsontosodrotbohm
authored andcommitted
DATAMONGO-1617 - BeforeConvertEvent is now emitted before updatable idendifier assertion.
We now make sure the BeforeConvertEvent is published before we check for identifier types that can potentially be auto-generated. That allows the event listeners to populate identifiers. Previously the identifier check kicked in before that and thus caused the listener not being able to populate the property. Original pull request: #443.
1 parent cd42b19 commit 5de3cb9

File tree

3 files changed

+116
-7
lines changed

3 files changed

+116
-7
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138
* @author Chuong Ngo
139139
* @author Christoph Strobl
140140
* @author Doménique Tilleuil
141+
* @author Laszlo Csontos
141142
*/
142143
@SuppressWarnings("deprecation")
143144
public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@@ -828,12 +829,11 @@ private WriteConcern potentiallyForceAcknowledgedWrite(WriteConcern wc) {
828829

829830
protected <T> void doInsert(String collectionName, T objectToSave, MongoWriter<T> writer) {
830831

832+
maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave, collectionName));
831833
assertUpdateableIdIfNotSet(objectToSave);
832834

833835
initializeVersionProperty(objectToSave);
834836

835-
maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave, collectionName));
836-
837837
DBObject dbDoc = toDbObject(objectToSave, writer);
838838

839839
maybeEmitEvent(new BeforeSaveEvent<T>(objectToSave, dbDoc, collectionName));
@@ -985,6 +985,7 @@ private <T> void doSaveVersioned(T objectToSave, MongoPersistentEntity<?> entity
985985
doInsert(collectionName, objectToSave, this.mongoConverter);
986986
} else {
987987

988+
maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave, collectionName));
988989
assertUpdateableIdIfNotSet(objectToSave);
989990

990991
// Create query for entity with the id and old version
@@ -996,7 +997,6 @@ private <T> void doSaveVersioned(T objectToSave, MongoPersistentEntity<?> entity
996997

997998
BasicDBObject dbObject = new BasicDBObject();
998999

999-
maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave, collectionName));
10001000
this.mongoConverter.write(objectToSave, dbObject);
10011001

10021002
maybeEmitEvent(new BeforeSaveEvent<T>(objectToSave, dbObject, collectionName));
@@ -1009,9 +1009,8 @@ private <T> void doSaveVersioned(T objectToSave, MongoPersistentEntity<?> entity
10091009

10101010
protected <T> void doSave(String collectionName, T objectToSave, MongoWriter<T> writer) {
10111011

1012-
assertUpdateableIdIfNotSet(objectToSave);
1013-
10141012
maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave, collectionName));
1013+
assertUpdateableIdIfNotSet(objectToSave);
10151014

10161015
DBObject dbDoc = toDbObject(objectToSave, writer);
10171016

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
import org.springframework.data.mongodb.core.mapping.Field;
7878
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
7979
import org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener;
80+
import org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent;
8081
import org.springframework.data.mongodb.core.mapping.event.BeforeSaveEvent;
8182
import org.springframework.data.mongodb.core.query.BasicQuery;
8283
import org.springframework.data.mongodb.core.query.Criteria;
@@ -113,6 +114,7 @@
113114
* @author Thomas Darimont
114115
* @author Komi Innocent
115116
* @author Christoph Strobl
117+
* @author Laszlo Csontos
116118
*/
117119
@RunWith(SpringJUnit4ClassRunner.class)
118120
@ContextConfiguration("classpath:infrastructure.xml")
@@ -127,13 +129,19 @@ public class MongoTemplateTests {
127129

128130
@Autowired MongoTemplate template;
129131
@Autowired MongoDbFactory factory;
130-
@Autowired ConfigurableApplicationContext context;
131132

133+
ConfigurableApplicationContext context;
132134
MongoTemplate mappingTemplate;
133135
org.springframework.data.util.Version mongoVersion;
134136

135137
@Rule public ExpectedException thrown = ExpectedException.none();
136138

139+
@Autowired
140+
public void setApplicationContext(ConfigurableApplicationContext context) {
141+
context.addApplicationListener(new PersonWithIdPropertyOfTypeUUIDListener());
142+
this.context = context;
143+
}
144+
137145
@Autowired
138146
public void setMongo(Mongo mongo) throws Exception {
139147

@@ -145,7 +153,8 @@ public void setMongo(Mongo mongo) throws Exception {
145153
PersonWith_idPropertyOfTypeString.class, PersonWithIdPropertyOfTypeObjectId.class,
146154
PersonWithIdPropertyOfTypeString.class, PersonWithIdPropertyOfTypeInteger.class,
147155
PersonWithIdPropertyOfTypeBigInteger.class, PersonWithIdPropertyOfPrimitiveInt.class,
148-
PersonWithIdPropertyOfTypeLong.class, PersonWithIdPropertyOfPrimitiveLong.class)));
156+
PersonWithIdPropertyOfTypeLong.class, PersonWithIdPropertyOfPrimitiveLong.class,
157+
PersonWithIdPropertyOfTypeUUID.class)));
149158
mappingContext.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
150159
mappingContext.initialize();
151160

@@ -188,6 +197,7 @@ protected void cleanDb() {
188197
template.dropCollection(PersonWithIdPropertyOfPrimitiveInt.class);
189198
template.dropCollection(PersonWithIdPropertyOfTypeLong.class);
190199
template.dropCollection(PersonWithIdPropertyOfPrimitiveLong.class);
200+
template.dropCollection(PersonWithIdPropertyOfTypeUUID.class);
191201
template.dropCollection(PersonWithVersionPropertyOfTypeInteger.class);
192202
template.dropCollection(TestClass.class);
193203
template.dropCollection(Sample.class);
@@ -643,6 +653,23 @@ private void testProperHandlingOfDifferentIdTypes(MongoTemplate mongoTemplate) t
643653
assertThat(p12q, notNullValue());
644654
assertThat(p12q.getId(), is(p12.getId()));
645655
checkCollectionContents(PersonWithIdPropertyOfPrimitiveLong.class, 1);
656+
657+
// DATAMONGO-1617
658+
// UUID id - provided
659+
PersonWithIdPropertyOfTypeUUID p13 = new PersonWithIdPropertyOfTypeUUID();
660+
p13.setFirstName("Sven_10");
661+
p13.setAge(22);
662+
p13.setId(UUID.randomUUID());
663+
// insert
664+
mongoTemplate.insert(p13);
665+
// also try save
666+
mongoTemplate.save(p13);
667+
assertThat(p13.getId(), notNullValue());
668+
PersonWithIdPropertyOfTypeUUID p13q = mongoTemplate.findOne(new Query(where("id").in(p13.getId())),
669+
PersonWithIdPropertyOfTypeUUID.class);
670+
assertThat(p13q, notNullValue());
671+
assertThat(p13q.getId(), is(p13.getId()));
672+
checkCollectionContents(PersonWithIdPropertyOfTypeUUID.class, 1);
646673
}
647674

648675
private void checkCollectionContents(Class<?> entityClass, int count) {
@@ -1497,6 +1524,17 @@ public void doesNotFailOnVersionInitForUnversionedEntity() {
14971524
template.insert(dbObject, template.determineCollectionName(PersonWithVersionPropertyOfTypeInteger.class));
14981525
}
14991526

1527+
@Test // DATAMONGO-1617
1528+
public void doesNotFailOnInsertForEntityWithNonAutogeneratableId() {
1529+
1530+
PersonWithIdPropertyOfTypeUUID person = new PersonWithIdPropertyOfTypeUUID();
1531+
person.setFirstName("Laszlo");
1532+
person.setAge(33);
1533+
1534+
template.insert(person);
1535+
assertThat(person.getId(), is(notNullValue()));
1536+
}
1537+
15001538
/**
15011539
* @see DATAMONGO-539
15021540
*/
@@ -3590,4 +3628,19 @@ static class WithObjectTypeProperty {
35903628
@Id String id;
35913629
Object value;
35923630
}
3631+
3632+
static class PersonWithIdPropertyOfTypeUUIDListener extends AbstractMongoEventListener<PersonWithIdPropertyOfTypeUUID> {
3633+
3634+
@Override
3635+
public void onBeforeConvert(BeforeConvertEvent<PersonWithIdPropertyOfTypeUUID> event) {
3636+
PersonWithIdPropertyOfTypeUUID person = event.getSource();
3637+
3638+
if (person.getId() != null) {
3639+
return;
3640+
}
3641+
3642+
person.setId(UUID.randomUUID());
3643+
}
3644+
3645+
}
35933646
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2010-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core;
17+
18+
import java.util.UUID;
19+
20+
public class PersonWithIdPropertyOfTypeUUID {
21+
22+
private UUID id;
23+
24+
private String firstName;
25+
26+
private int age;
27+
28+
public UUID getId() {
29+
return id;
30+
}
31+
32+
public void setId(UUID id) {
33+
this.id = id;
34+
}
35+
36+
public String getFirstName() {
37+
return firstName;
38+
}
39+
40+
public void setFirstName(String firstName) {
41+
this.firstName = firstName;
42+
}
43+
44+
public int getAge() {
45+
return age;
46+
}
47+
48+
public void setAge(int age) {
49+
this.age = age;
50+
}
51+
52+
@Override
53+
public String toString() {
54+
return "PersonWithIdPropertyOfTypeUUID [id=" + id + ", firstName=" + firstName + ", age=" + age + "]";
55+
}
56+
57+
}

0 commit comments

Comments
 (0)