DATADOC-136 - Fixed conversion of enums.
Introduced writeSimpleInternal(…) method that automatically stores the name of an enum instead of the enum itself. Changed quite a few places to rather use MongoPersistentProperty.getKey() over getName().
This commit is contained in:
@@ -397,11 +397,9 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
// Write the properties
|
||||
entity.doWithProperties(new PropertyHandler<MongoPersistentProperty>() {
|
||||
public void doWithPersistentProperty(MongoPersistentProperty prop) {
|
||||
String name = prop.getName();
|
||||
Class<?> type = prop.getType();
|
||||
Object propertyObj;
|
||||
try {
|
||||
propertyObj = getProperty(obj, prop, type, useFieldAccessOnly);
|
||||
propertyObj = getProperty(obj, prop, prop.getType(), useFieldAccessOnly);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new MappingException(e.getMessage(), e);
|
||||
} catch (InvocationTargetException e) {
|
||||
@@ -411,7 +409,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
if (!isSimpleType(propertyObj.getClass())) {
|
||||
writePropertyInternal(prop, propertyObj, dbo);
|
||||
} else {
|
||||
dbo.put(name, propertyObj);
|
||||
writeSimpleInternal(prop.getKey(), propertyObj, dbo);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -467,7 +465,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
org.springframework.data.document.mongodb.mapping.DBRef dbref = prop.getField().getAnnotation(
|
||||
org.springframework.data.document.mongodb.mapping.DBRef.class);
|
||||
|
||||
String name = prop.getName();
|
||||
String name = prop.getKey();
|
||||
Class<?> type = prop.getType();
|
||||
if (prop.isCollection()) {
|
||||
BasicDBList dbList = new BasicDBList();
|
||||
@@ -552,7 +550,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
// being convertable
|
||||
String simpleKey = key.toString();
|
||||
if (isSimpleType(val.getClass())) {
|
||||
dbo.put(simpleKey, val);
|
||||
writeSimpleInternal(simpleKey, val, dbo);
|
||||
} else {
|
||||
DBObject newDbo = new BasicDBObject();
|
||||
Class<?> componentType = val.getClass();
|
||||
@@ -571,6 +569,19 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the given simple value to the given {@link DBObject}. Will store enum names for enum values.
|
||||
*
|
||||
* @param key
|
||||
* @param value
|
||||
* @param dbObject
|
||||
*/
|
||||
private void writeSimpleInternal(String key, Object value, DBObject dbObject) {
|
||||
|
||||
Object valueToSet = value.getClass().isEnum() ? ((Enum<?>) value).name() : value;
|
||||
dbObject.put(key, valueToSet);
|
||||
}
|
||||
|
||||
protected DBRef createDBRef(Object target, org.springframework.data.document.mongodb.mapping.DBRef dbref) {
|
||||
MongoPersistentEntity<?> targetEntity = mappingContext.getPersistentEntity(target.getClass());
|
||||
|
||||
@@ -89,7 +89,7 @@ public class MappingMongoConverterUnitTests {
|
||||
DBObject dbObject = new BasicDBObject();
|
||||
converter.write(person, dbObject);
|
||||
|
||||
assertTrue(dbObject.get("birthDate") instanceof Date);
|
||||
assertThat(dbObject.get("birthDate"), is(Date.class));
|
||||
|
||||
Person result = converter.read(Person.class, dbObject);
|
||||
assertThat(result.birthDate, is(notNullValue()));
|
||||
@@ -161,6 +161,43 @@ public class MappingMongoConverterUnitTests {
|
||||
assertThat(result.get(MappingMongoConverter.CUSTOM_TYPE_KEY).toString(), is(Person.class.getName()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATADOC-136
|
||||
*/
|
||||
@Test
|
||||
public void writesEnumsCorrectly() {
|
||||
|
||||
ClassWithEnumProperty value = new ClassWithEnumProperty();
|
||||
value.sampleEnum = SampleEnum.FIRST;
|
||||
|
||||
DBObject result = new BasicDBObject();
|
||||
converter.write(value, result);
|
||||
|
||||
assertThat(result.get("sampleEnum"), is(String.class));
|
||||
assertThat(result.get("sampleEnum").toString(), is("FIRST"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATADOC-136
|
||||
*/
|
||||
@Test
|
||||
public void readsEnumsCorrectly() {
|
||||
DBObject dbObject = new BasicDBObject("sampleEnum", "FIRST");
|
||||
ClassWithEnumProperty result = converter.read(ClassWithEnumProperty.class, dbObject);
|
||||
|
||||
assertThat(result.sampleEnum, is(SampleEnum.FIRST));
|
||||
}
|
||||
|
||||
|
||||
class ClassWithEnumProperty {
|
||||
|
||||
SampleEnum sampleEnum;
|
||||
}
|
||||
|
||||
enum SampleEnum {
|
||||
FIRST, SECOND;
|
||||
}
|
||||
|
||||
public static class Address {
|
||||
String street;
|
||||
String city;
|
||||
|
||||
@@ -16,6 +16,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.document.mongodb.geo.Box;
|
||||
import org.springframework.data.document.mongodb.geo.Circle;
|
||||
import org.springframework.data.document.mongodb.geo.Point;
|
||||
import org.springframework.data.document.mongodb.repository.Person.Sex;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort.Direction;
|
||||
@@ -31,8 +32,10 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
protected PersonRepository repository;
|
||||
Person dave, carter, boyd, stefan, leroi;
|
||||
Person dave, carter, boyd, stefan, leroi, alicia;
|
||||
QPerson person;
|
||||
|
||||
List<Person> all;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
@@ -44,10 +47,12 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
|
||||
boyd = new Person("Boyd", "Tinsley", 45);
|
||||
stefan = new Person("Stefan", "Lessard", 34);
|
||||
leroi = new Person("Leroi", "Moore", 41);
|
||||
|
||||
alicia = new Person("Alicia", "Keys", 30, Sex.FEMALE);
|
||||
|
||||
person = new QPerson("person");
|
||||
|
||||
repository.save(Arrays.asList(dave, carter, boyd, stefan, leroi));
|
||||
all = repository.save(Arrays.asList(dave, carter, boyd, stefan, leroi, alicia));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -59,8 +64,8 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
|
||||
@Test
|
||||
public void findsAllMusicians() throws Exception {
|
||||
List<Person> result = repository.findAll();
|
||||
assertThat(result, hasItems(dave, carter, boyd, stefan, leroi));
|
||||
assertThat(result.size(), is(5));
|
||||
assertThat(result.size(), is(all.size()));
|
||||
assertThat(result.containsAll(all), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -70,7 +75,7 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
|
||||
|
||||
List<Person> result = repository.findAll();
|
||||
|
||||
assertThat(result.size(), is(4));
|
||||
assertThat(result.size(), is(all.size() - 1));
|
||||
assertThat(result, not(hasItem(dave)));
|
||||
}
|
||||
|
||||
@@ -81,7 +86,7 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
|
||||
|
||||
List<Person> result = repository.findAll();
|
||||
|
||||
assertThat(result.size(), is(4));
|
||||
assertThat(result.size(), is(all.size() - 1));
|
||||
assertThat(result, not(hasItem(dave)));
|
||||
}
|
||||
|
||||
@@ -116,7 +121,8 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
|
||||
Page<Person> result = repository.findAll(new PageRequest(1, 2, Direction.ASC, "lastname"));
|
||||
assertThat(result.isFirstPage(), is(false));
|
||||
assertThat(result.isLastPage(), is(false));
|
||||
assertThat(result, hasItems(dave, leroi));
|
||||
assertThat(result, hasItems(dave, stefan));
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -237,4 +243,15 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
|
||||
assertThat(page.getNumberOfElements(), is(2));
|
||||
assertThat(page, hasItems(carter, stefan));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATADOC-136
|
||||
*/
|
||||
@Test
|
||||
public void findsPeopleBySexCorrectly() {
|
||||
|
||||
List<Person> females = repository.findBySex(Sex.FEMALE);
|
||||
assertThat(females.size(), is(1));
|
||||
assertThat(females.get(0), is(alicia));
|
||||
}
|
||||
}
|
||||
@@ -29,9 +29,14 @@ import org.springframework.data.document.mongodb.mapping.Document;
|
||||
@Document
|
||||
public class Person extends Contact {
|
||||
|
||||
public enum Sex {
|
||||
MALE, FEMALE;
|
||||
}
|
||||
|
||||
private String firstname;
|
||||
private String lastname;
|
||||
private Integer age;
|
||||
private Sex sex;
|
||||
|
||||
@GeoSpatialIndexed
|
||||
private Point location;
|
||||
@@ -51,10 +56,16 @@ public class Person extends Contact {
|
||||
|
||||
public Person(String firstname, String lastname, Integer age) {
|
||||
|
||||
this(firstname, lastname, age, Sex.MALE);
|
||||
}
|
||||
|
||||
public Person(String firstname, String lastname, Integer age, Sex sex) {
|
||||
|
||||
super();
|
||||
this.firstname = firstname;
|
||||
this.lastname = lastname;
|
||||
this.age = age;
|
||||
this.sex = sex;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -21,6 +21,7 @@ import java.util.List;
|
||||
import org.springframework.data.document.mongodb.geo.Box;
|
||||
import org.springframework.data.document.mongodb.geo.Circle;
|
||||
import org.springframework.data.document.mongodb.geo.Point;
|
||||
import org.springframework.data.document.mongodb.repository.Person.Sex;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
|
||||
@@ -118,4 +119,6 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
|
||||
List<Person> findByLocationWithin(Circle circle);
|
||||
|
||||
List<Person> findByLocationWithin(Box box);
|
||||
|
||||
List<Person> findBySex(Sex sex);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user