Skip to content

Commit 781717f

Browse files
laszlocsontoschristophstrobl
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 084a167 commit 781717f

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
@@ -143,6 +143,7 @@
143143
* @author Doménique Tilleuil
144144
* @author Niko Schmuck
145145
* @author Mark Paluch
146+
* @author Laszlo Csontos
146147
*/
147148
@SuppressWarnings("deprecation")
148149
public class MongoTemplate implements MongoOperations, ApplicationContextAware, IndexOperationsProvider {
@@ -832,12 +833,11 @@ private WriteConcern potentiallyForceAcknowledgedWrite(WriteConcern wc) {
832833

833834
protected <T> void doInsert(String collectionName, T objectToSave, MongoWriter<T> writer) {
834835

836+
maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave, collectionName));
835837
assertUpdateableIdIfNotSet(objectToSave);
836838

837839
initializeVersionProperty(objectToSave);
838840

839-
maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave, collectionName));
840-
841841
Document dbDoc = toDocument(objectToSave, writer);
842842

843843
maybeEmitEvent(new BeforeSaveEvent<T>(objectToSave, dbDoc, collectionName));
@@ -998,6 +998,7 @@ private <T> void doSaveVersioned(T objectToSave, MongoPersistentEntity<?> entity
998998
doInsert(collectionName, objectToSave, this.mongoConverter);
999999
} else {
10001000

1001+
maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave, collectionName));
10011002
assertUpdateableIdIfNotSet(objectToSave);
10021003

10031004
// Create query for entity with the id and old version
@@ -1009,7 +1010,6 @@ private <T> void doSaveVersioned(T objectToSave, MongoPersistentEntity<?> entity
10091010

10101011
Document document = new Document();
10111012

1012-
maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave, collectionName));
10131013
this.mongoConverter.write(objectToSave, document);
10141014

10151015
maybeEmitEvent(new BeforeSaveEvent<T>(objectToSave, document, collectionName));
@@ -1028,9 +1028,8 @@ private <T> void doSaveVersioned(T objectToSave, MongoPersistentEntity<?> entity
10281028

10291029
protected <T> void doSave(String collectionName, T objectToSave, MongoWriter<T> writer) {
10301030

1031-
assertUpdateableIdIfNotSet(objectToSave);
1032-
10331031
maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave, collectionName));
1032+
assertUpdateableIdIfNotSet(objectToSave);
10341033

10351034
Document dbDoc = toDocument(objectToSave, writer);
10361035

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;
@@ -118,6 +119,7 @@
118119
* @author Komi Innocent
119120
* @author Christoph Strobl
120121
* @author Mark Paluch
122+
* @author Laszlo Csontos
121123
*/
122124
@RunWith(SpringJUnit4ClassRunner.class)
123125
@ContextConfiguration("classpath:infrastructure.xml")
@@ -132,13 +134,19 @@ public class MongoTemplateTests {
132134

133135
@Autowired MongoTemplate template;
134136
@Autowired MongoDbFactory factory;
135-
@Autowired ConfigurableApplicationContext context;
136137

138+
ConfigurableApplicationContext context;
137139
MongoTemplate mappingTemplate;
138140
org.springframework.data.util.Version mongoVersion;
139141

140142
@Rule public ExpectedException thrown = ExpectedException.none();
141143

144+
@Autowired
145+
public void setApplicationContext(ConfigurableApplicationContext context) {
146+
context.addApplicationListener(new PersonWithIdPropertyOfTypeUUIDListener());
147+
this.context = context;
148+
}
149+
142150
@Autowired
143151
public void setMongo(Mongo mongo) throws Exception {
144152

@@ -150,7 +158,8 @@ public void setMongo(Mongo mongo) throws Exception {
150158
PersonWith_idPropertyOfTypeString.class, PersonWithIdPropertyOfTypeObjectId.class,
151159
PersonWithIdPropertyOfTypeString.class, PersonWithIdPropertyOfTypeInteger.class,
152160
PersonWithIdPropertyOfTypeBigInteger.class, PersonWithIdPropertyOfPrimitiveInt.class,
153-
PersonWithIdPropertyOfTypeLong.class, PersonWithIdPropertyOfPrimitiveLong.class)));
161+
PersonWithIdPropertyOfTypeLong.class, PersonWithIdPropertyOfPrimitiveLong.class,
162+
PersonWithIdPropertyOfTypeUUID.class)));
154163
mappingContext.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
155164
mappingContext.initialize();
156165

@@ -193,6 +202,7 @@ protected void cleanDb() {
193202
template.dropCollection(PersonWithIdPropertyOfPrimitiveInt.class);
194203
template.dropCollection(PersonWithIdPropertyOfTypeLong.class);
195204
template.dropCollection(PersonWithIdPropertyOfPrimitiveLong.class);
205+
template.dropCollection(PersonWithIdPropertyOfTypeUUID.class);
196206
template.dropCollection(PersonWithVersionPropertyOfTypeInteger.class);
197207
template.dropCollection(TestClass.class);
198208
template.dropCollection(Sample.class);
@@ -627,6 +637,23 @@ private void testProperHandlingOfDifferentIdTypes(MongoTemplate mongoTemplate) t
627637
assertThat(p12q, notNullValue());
628638
assertThat(p12q.getId(), is(p12.getId()));
629639
checkCollectionContents(PersonWithIdPropertyOfPrimitiveLong.class, 1);
640+
641+
// DATAMONGO-1617
642+
// UUID id - provided
643+
PersonWithIdPropertyOfTypeUUID p13 = new PersonWithIdPropertyOfTypeUUID();
644+
p13.setFirstName("Sven_10");
645+
p13.setAge(22);
646+
p13.setId(UUID.randomUUID());
647+
// insert
648+
mongoTemplate.insert(p13);
649+
// also try save
650+
mongoTemplate.save(p13);
651+
assertThat(p13.getId(), notNullValue());
652+
PersonWithIdPropertyOfTypeUUID p13q = mongoTemplate.findOne(new Query(where("id").in(p13.getId())),
653+
PersonWithIdPropertyOfTypeUUID.class);
654+
assertThat(p13q, notNullValue());
655+
assertThat(p13q.getId(), is(p13.getId()));
656+
checkCollectionContents(PersonWithIdPropertyOfTypeUUID.class, 1);
630657
}
631658

632659
private void checkCollectionContents(Class<?> entityClass, int count) {
@@ -1427,6 +1454,17 @@ public void doesNotFailOnVersionInitForUnversionedEntity() {
14271454
template.insert(document, template.determineCollectionName(PersonWithVersionPropertyOfTypeInteger.class));
14281455
}
14291456

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

@@ -3570,4 +3608,19 @@ static class WithObjectTypeProperty {
35703608
@Id String id;
35713609
Object value;
35723610
}
3611+
3612+
static class PersonWithIdPropertyOfTypeUUIDListener extends AbstractMongoEventListener<PersonWithIdPropertyOfTypeUUID> {
3613+
3614+
@Override
3615+
public void onBeforeConvert(BeforeConvertEvent<PersonWithIdPropertyOfTypeUUID> event) {
3616+
PersonWithIdPropertyOfTypeUUID person = event.getSource();
3617+
3618+
if (person.getId() != null) {
3619+
return;
3620+
}
3621+
3622+
person.setId(UUID.randomUUID());
3623+
}
3624+
3625+
}
35733626
}
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)