Skip to content

Commit 593c536

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 9c06124 commit 593c536

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
@@ -141,6 +141,7 @@
141141
* @author Doménique Tilleuil
142142
* @author Niko Schmuck
143143
* @author Mark Paluch
144+
* @author Laszlo Csontos
144145
*/
145146
@SuppressWarnings("deprecation")
146147
public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@@ -844,12 +845,11 @@ private WriteConcern potentiallyForceAcknowledgedWrite(WriteConcern wc) {
844845

845846
protected <T> void doInsert(String collectionName, T objectToSave, MongoWriter<T> writer) {
846847

848+
maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave, collectionName));
847849
assertUpdateableIdIfNotSet(objectToSave);
848850

849851
initializeVersionProperty(objectToSave);
850852

851-
maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave, collectionName));
852-
853853
DBObject dbDoc = toDbObject(objectToSave, writer);
854854

855855
maybeEmitEvent(new BeforeSaveEvent<T>(objectToSave, dbDoc, collectionName));
@@ -1001,6 +1001,7 @@ private <T> void doSaveVersioned(T objectToSave, MongoPersistentEntity<?> entity
10011001
doInsert(collectionName, objectToSave, this.mongoConverter);
10021002
} else {
10031003

1004+
maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave, collectionName));
10041005
assertUpdateableIdIfNotSet(objectToSave);
10051006

10061007
// Create query for entity with the id and old version
@@ -1012,7 +1013,6 @@ private <T> void doSaveVersioned(T objectToSave, MongoPersistentEntity<?> entity
10121013

10131014
BasicDBObject dbObject = new BasicDBObject();
10141015

1015-
maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave, collectionName));
10161016
this.mongoConverter.write(objectToSave, dbObject);
10171017

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

10261026
protected <T> void doSave(String collectionName, T objectToSave, MongoWriter<T> writer) {
10271027

1028-
assertUpdateableIdIfNotSet(objectToSave);
1029-
10301028
maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave, collectionName));
1029+
assertUpdateableIdIfNotSet(objectToSave);
10311030

10321031
DBObject dbDoc = toDbObject(objectToSave, writer);
10331032

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
@@ -82,6 +82,7 @@
8282
import org.springframework.data.mongodb.core.mapping.Field;
8383
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
8484
import org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener;
85+
import org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent;
8586
import org.springframework.data.mongodb.core.mapping.event.BeforeSaveEvent;
8687
import org.springframework.data.mongodb.core.query.BasicQuery;
8788
import org.springframework.data.mongodb.core.query.Criteria;
@@ -119,6 +120,7 @@
119120
* @author Komi Innocent
120121
* @author Christoph Strobl
121122
* @author Mark Paluch
123+
* @author Laszlo Csontos
122124
*/
123125
@RunWith(SpringJUnit4ClassRunner.class)
124126
@ContextConfiguration("classpath:infrastructure.xml")
@@ -134,13 +136,19 @@ public class MongoTemplateTests {
134136

135137
@Autowired MongoTemplate template;
136138
@Autowired MongoDbFactory factory;
137-
@Autowired ConfigurableApplicationContext context;
138139

140+
ConfigurableApplicationContext context;
139141
MongoTemplate mappingTemplate;
140142
org.springframework.data.util.Version mongoVersion;
141143

142144
@Rule public ExpectedException thrown = ExpectedException.none();
143145

146+
@Autowired
147+
public void setApplicationContext(ConfigurableApplicationContext context) {
148+
context.addApplicationListener(new PersonWithIdPropertyOfTypeUUIDListener());
149+
this.context = context;
150+
}
151+
144152
@Autowired
145153
public void setMongo(Mongo mongo) throws Exception {
146154

@@ -152,7 +160,8 @@ public void setMongo(Mongo mongo) throws Exception {
152160
PersonWith_idPropertyOfTypeString.class, PersonWithIdPropertyOfTypeObjectId.class,
153161
PersonWithIdPropertyOfTypeString.class, PersonWithIdPropertyOfTypeInteger.class,
154162
PersonWithIdPropertyOfTypeBigInteger.class, PersonWithIdPropertyOfPrimitiveInt.class,
155-
PersonWithIdPropertyOfTypeLong.class, PersonWithIdPropertyOfPrimitiveLong.class)));
163+
PersonWithIdPropertyOfTypeLong.class, PersonWithIdPropertyOfPrimitiveLong.class,
164+
PersonWithIdPropertyOfTypeUUID.class)));
156165
mappingContext.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
157166
mappingContext.initialize();
158167

@@ -195,6 +204,7 @@ protected void cleanDb() {
195204
template.dropCollection(PersonWithIdPropertyOfPrimitiveInt.class);
196205
template.dropCollection(PersonWithIdPropertyOfTypeLong.class);
197206
template.dropCollection(PersonWithIdPropertyOfPrimitiveLong.class);
207+
template.dropCollection(PersonWithIdPropertyOfTypeUUID.class);
198208
template.dropCollection(PersonWithVersionPropertyOfTypeInteger.class);
199209
template.dropCollection(TestClass.class);
200210
template.dropCollection(Sample.class);
@@ -632,6 +642,23 @@ private void testProperHandlingOfDifferentIdTypes(MongoTemplate mongoTemplate) t
632642
assertThat(p12q, notNullValue());
633643
assertThat(p12q.getId(), is(p12.getId()));
634644
checkCollectionContents(PersonWithIdPropertyOfPrimitiveLong.class, 1);
645+
646+
// DATAMONGO-1617
647+
// UUID id - provided
648+
PersonWithIdPropertyOfTypeUUID p13 = new PersonWithIdPropertyOfTypeUUID();
649+
p13.setFirstName("Sven_10");
650+
p13.setAge(22);
651+
p13.setId(UUID.randomUUID());
652+
// insert
653+
mongoTemplate.insert(p13);
654+
// also try save
655+
mongoTemplate.save(p13);
656+
assertThat(p13.getId(), notNullValue());
657+
PersonWithIdPropertyOfTypeUUID p13q = mongoTemplate.findOne(new Query(where("id").in(p13.getId())),
658+
PersonWithIdPropertyOfTypeUUID.class);
659+
assertThat(p13q, notNullValue());
660+
assertThat(p13q.getId(), is(p13.getId()));
661+
checkCollectionContents(PersonWithIdPropertyOfTypeUUID.class, 1);
635662
}
636663

637664
private void checkCollectionContents(Class<?> entityClass, int count) {
@@ -1429,6 +1456,17 @@ public void doesNotFailOnVersionInitForUnversionedEntity() {
14291456
template.insert(dbObject, template.determineCollectionName(PersonWithVersionPropertyOfTypeInteger.class));
14301457
}
14311458

1459+
@Test // DATAMONGO-1617
1460+
public void doesNotFailOnInsertForEntityWithNonAutogeneratableId() {
1461+
1462+
PersonWithIdPropertyOfTypeUUID person = new PersonWithIdPropertyOfTypeUUID();
1463+
person.setFirstName("Laszlo");
1464+
person.setAge(33);
1465+
1466+
template.insert(person);
1467+
assertThat(person.getId(), is(notNullValue()));
1468+
}
1469+
14321470
@Test // DATAMONGO-539
14331471
public void removesObjectFromExplicitCollection() {
14341472

@@ -3545,4 +3583,19 @@ static class WithObjectTypeProperty {
35453583
@Id String id;
35463584
Object value;
35473585
}
3586+
3587+
static class PersonWithIdPropertyOfTypeUUIDListener extends AbstractMongoEventListener<PersonWithIdPropertyOfTypeUUID> {
3588+
3589+
@Override
3590+
public void onBeforeConvert(BeforeConvertEvent<PersonWithIdPropertyOfTypeUUID> event) {
3591+
PersonWithIdPropertyOfTypeUUID person = event.getSource();
3592+
3593+
if (person.getId() != null) {
3594+
return;
3595+
}
3596+
3597+
person.setId(UUID.randomUUID());
3598+
}
3599+
3600+
}
35483601
}
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)