Skip to content

DATAMONGO-1617 - IDs with non-autogeneratable type cannot be assigned automatically with custom event listeners #443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
* @author Doménique Tilleuil
* @author Niko Schmuck
* @author Mark Paluch
* @author Laszlo Csontos
*/
@SuppressWarnings("deprecation")
public class MongoTemplate implements MongoOperations, ApplicationContextAware {
Expand Down Expand Up @@ -844,12 +845,11 @@ private WriteConcern potentiallyForceAcknowledgedWrite(WriteConcern wc) {

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

maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave, collectionName));
assertUpdateableIdIfNotSet(objectToSave);

initializeVersionProperty(objectToSave);

maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave, collectionName));

DBObject dbDoc = toDbObject(objectToSave, writer);

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

maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave, collectionName));
assertUpdateableIdIfNotSet(objectToSave);

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

BasicDBObject dbObject = new BasicDBObject();

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

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

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

assertUpdateableIdIfNotSet(objectToSave);

maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave, collectionName));
assertUpdateableIdIfNotSet(objectToSave);

DBObject dbDoc = toDbObject(objectToSave, writer);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
import org.springframework.data.mongodb.core.mapping.Field;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener;
import org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent;
import org.springframework.data.mongodb.core.mapping.event.BeforeSaveEvent;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Criteria;
Expand Down Expand Up @@ -119,6 +120,7 @@
* @author Komi Innocent
* @author Christoph Strobl
* @author Mark Paluch
* @author Laszlo Csontos
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:infrastructure.xml")
Expand All @@ -134,13 +136,19 @@ public class MongoTemplateTests {

@Autowired MongoTemplate template;
@Autowired MongoDbFactory factory;
@Autowired ConfigurableApplicationContext context;

ConfigurableApplicationContext context;
MongoTemplate mappingTemplate;
org.springframework.data.util.Version mongoVersion;

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

@Autowired
public void setApplicationContext(ConfigurableApplicationContext context) {
context.addApplicationListener(new PersonWithIdPropertyOfTypeUUIDListener());
this.context = context;
}

@Autowired
public void setMongo(Mongo mongo) throws Exception {

Expand All @@ -152,7 +160,8 @@ public void setMongo(Mongo mongo) throws Exception {
PersonWith_idPropertyOfTypeString.class, PersonWithIdPropertyOfTypeObjectId.class,
PersonWithIdPropertyOfTypeString.class, PersonWithIdPropertyOfTypeInteger.class,
PersonWithIdPropertyOfTypeBigInteger.class, PersonWithIdPropertyOfPrimitiveInt.class,
PersonWithIdPropertyOfTypeLong.class, PersonWithIdPropertyOfPrimitiveLong.class)));
PersonWithIdPropertyOfTypeLong.class, PersonWithIdPropertyOfPrimitiveLong.class,
PersonWithIdPropertyOfTypeUUID.class)));
mappingContext.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
mappingContext.initialize();

Expand Down Expand Up @@ -195,6 +204,7 @@ protected void cleanDb() {
template.dropCollection(PersonWithIdPropertyOfPrimitiveInt.class);
template.dropCollection(PersonWithIdPropertyOfTypeLong.class);
template.dropCollection(PersonWithIdPropertyOfPrimitiveLong.class);
template.dropCollection(PersonWithIdPropertyOfTypeUUID.class);
template.dropCollection(PersonWithVersionPropertyOfTypeInteger.class);
template.dropCollection(TestClass.class);
template.dropCollection(Sample.class);
Expand Down Expand Up @@ -632,6 +642,23 @@ private void testProperHandlingOfDifferentIdTypes(MongoTemplate mongoTemplate) t
assertThat(p12q, notNullValue());
assertThat(p12q.getId(), is(p12.getId()));
checkCollectionContents(PersonWithIdPropertyOfPrimitiveLong.class, 1);

// DATAMONGO-1617
// UUID id - provided
PersonWithIdPropertyOfTypeUUID p13 = new PersonWithIdPropertyOfTypeUUID();
p13.setFirstName("Sven_10");
p13.setAge(22);
p13.setId(UUID.randomUUID());
// insert
mongoTemplate.insert(p13);
// also try save
mongoTemplate.save(p13);
assertThat(p13.getId(), notNullValue());
PersonWithIdPropertyOfTypeUUID p13q = mongoTemplate.findOne(new Query(where("id").in(p13.getId())),
PersonWithIdPropertyOfTypeUUID.class);
assertThat(p13q, notNullValue());
assertThat(p13q.getId(), is(p13.getId()));
checkCollectionContents(PersonWithIdPropertyOfTypeUUID.class, 1);
}

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

@Test // DATAMONGO-1617
public void doesNotFailOnInsertForEntityWithNonAutogeneratableId() {

PersonWithIdPropertyOfTypeUUID person = new PersonWithIdPropertyOfTypeUUID();
person.setFirstName("Laszlo");
person.setAge(33);

template.insert(person);
assertThat(person.getId(), is(notNullValue()));
}

@Test // DATAMONGO-539
public void removesObjectFromExplicitCollection() {

Expand Down Expand Up @@ -3545,4 +3583,19 @@ static class WithObjectTypeProperty {
@Id String id;
Object value;
}

static class PersonWithIdPropertyOfTypeUUIDListener extends AbstractMongoEventListener<PersonWithIdPropertyOfTypeUUID> {

@Override
public void onBeforeConvert(BeforeConvertEvent<PersonWithIdPropertyOfTypeUUID> event) {
PersonWithIdPropertyOfTypeUUID person = event.getSource();

if (person.getId() != null) {
return;
}

person.setId(UUID.randomUUID());
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2010-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.core;

import java.util.UUID;

public class PersonWithIdPropertyOfTypeUUID {

private UUID id;

private String firstName;

private int age;

public UUID getId() {
return id;
}

public void setId(UUID id) {
this.id = id;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

@Override
public String toString() {
return "PersonWithIdPropertyOfTypeUUID [id=" + id + ", firstName=" + firstName + ", age=" + age + "]";
}

}