Preparing to merge into branch master

This commit is contained in:
Jon Brisbin
2011-03-15 09:01:29 -05:00
committed by J. Brisbin
parent 784ee634b6
commit 65d7a7feaf
8 changed files with 247 additions and 154 deletions

View File

@@ -115,31 +115,6 @@
<version>1.0.0.Final</version> <version>1.0.0.Final</version>
</dependency> </dependency>
<!-- For Tests -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.5.5-Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>1.8.0.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.0.2.GA</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<repositories> <repositories>
<repository> <repository>
@@ -207,4 +182,5 @@
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
</project> </project>

View File

@@ -36,6 +36,7 @@
<dependency> <dependency>
<groupId>org.springframework.data</groupId> <groupId>org.springframework.data</groupId>
<artifactId>spring-data-document-core</artifactId> <artifactId>spring-data-document-core</artifactId>
<version>${project.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.data</groupId> <groupId>org.springframework.data</groupId>

View File

@@ -33,11 +33,13 @@ import org.springframework.core.convert.ConversionFailedException;
import org.springframework.dao.DataAccessException; import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException; import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.data.document.mongodb.MongoPropertyDescriptors.MongoPropertyDescriptor; import org.springframework.data.document.mongodb.MongoPropertyDescriptors.MongoPropertyDescriptor;
import org.springframework.data.document.mongodb.convert.MappingMongoConverter;
import org.springframework.data.document.mongodb.query.IndexDefinition; import org.springframework.data.document.mongodb.query.IndexDefinition;
import org.springframework.data.document.mongodb.query.Query; import org.springframework.data.document.mongodb.query.Query;
import org.springframework.data.document.mongodb.query.Update; import org.springframework.data.document.mongodb.query.Update;
import org.springframework.data.document.mongodb.convert.MongoConverter; import org.springframework.data.document.mongodb.convert.MongoConverter;
import org.springframework.data.document.mongodb.convert.SimpleMongoConverter; import org.springframework.data.document.mongodb.convert.SimpleMongoConverter;
import org.springframework.data.mapping.model.MappingConfigurationBuilder;
import org.springframework.jca.cci.core.ConnectionCallback; import org.springframework.jca.cci.core.ConnectionCallback;
import org.springframework.util.Assert; import org.springframework.util.Assert;

View File

@@ -174,7 +174,7 @@ public class MappingMongoConverter implements MongoConverter, ApplicationContext
}, spelCtx); }, spelCtx);
// Set the ID // Set the ID
PersistentProperty<?> idProperty = entity.getIdProperty(); final PersistentProperty<?> idProperty = entity.getIdProperty();
if (dbo.containsField("_id") || null != idProperty) { if (dbo.containsField("_id") || null != idProperty) {
Object idObj = dbo.get("_id"); Object idObj = dbo.get("_id");
try { try {
@@ -189,7 +189,17 @@ public class MappingMongoConverter implements MongoConverter, ApplicationContext
// Set properties not already set in the constructor // Set properties not already set in the constructor
entity.doWithProperties(new PropertyHandler() { entity.doWithProperties(new PropertyHandler() {
public void doWithPersistentProperty(PersistentProperty<?> prop) { public void doWithPersistentProperty(PersistentProperty<?> prop) {
if (!ctorParamNames.contains(prop.getName())) { String name = prop.getName();
if (null != idProperty && name.equals(idProperty.getName())) {
return;
}
if (prop.isAssociation()) {
return;
}
if (ctorParamNames.contains(prop.getName())) {
return;
}
Object obj = getValueInternal(prop, dbo, spelCtx, prop.getValueAnnotation()); Object obj = getValueInternal(prop, dbo, spelCtx, prop.getValueAnnotation());
try { try {
MappingBeanHelper.setProperty(instance, prop, obj, useFieldAccessOnly); MappingBeanHelper.setProperty(instance, prop, obj, useFieldAccessOnly);
@@ -199,7 +209,6 @@ public class MappingMongoConverter implements MongoConverter, ApplicationContext
throw new MappingException(e.getMessage(), e); throw new MappingException(e.getMessage(), e);
} }
} }
}
}); });
// Handle associations // Handle associations
@@ -343,6 +352,7 @@ public class MappingMongoConverter implements MongoConverter, ApplicationContext
} }
} else { } else {
BasicDBObject propDbObj = new BasicDBObject(); BasicDBObject propDbObj = new BasicDBObject();
//dbo.put("_class", prop.getType().getName());
write(propObjItem, propDbObj); write(propObjItem, propDbObj);
dbList.add(propDbObj); dbList.add(propDbObj);
} }
@@ -376,6 +386,15 @@ public class MappingMongoConverter implements MongoConverter, ApplicationContext
dbo.put(simpleKey, val); dbo.put(simpleKey, val);
} else { } else {
DBObject newDbo = new BasicDBObject(); DBObject newDbo = new BasicDBObject();
Class<?> componentType = val.getClass();
if (componentType.isArray()
|| componentType.isAssignableFrom(Collection.class)
|| componentType.isAssignableFrom(List.class)) {
Class<?> ctype = val.getClass().getComponentType();
dbo.put("_class", (null != ctype ? ctype.getName() : componentType.getName()));
} else {
dbo.put("_class", componentType.getName());
}
write(val, newDbo); write(val, newDbo);
dbo.put(simpleKey, newDbo); dbo.put(simpleKey, newDbo);
} }
@@ -434,10 +453,16 @@ public class MappingMongoConverter implements MongoConverter, ApplicationContext
if (type.isAssignableFrom(Map.class) && dbObj instanceof DBObject) { if (type.isAssignableFrom(Map.class) && dbObj instanceof DBObject) {
Map m = new LinkedHashMap(); Map m = new LinkedHashMap();
for (Map.Entry<String, Object> entry : ((Map<String, Object>) ((DBObject) dbObj).toMap()).entrySet()) { for (Map.Entry<String, Object> entry : ((Map<String, Object>) ((DBObject) dbObj).toMap()).entrySet()) {
if (null != entry.getValue() Class<?> toType = null;
&& MappingBeanHelper.getSimpleTypes().contains(entry.getValue().getClass().getName())) { if (entry.getKey().equals("_class")) {
m.put(entry.getKey(), entry.getValue()); try {
} else if (null != entry.getValue()) { toType = Class.forName(entry.getValue().toString());
} catch (ClassNotFoundException e) {
throw new MappingException(e.getMessage(), e);
}
} else if (null != entry.getValue() && entry.getValue() instanceof DBObject) {
m.put(entry.getKey(), read((null != toType ? toType : type), (DBObject) entry.getValue()));
} else {
m.put(entry.getKey(), entry.getValue()); m.put(entry.getKey(), entry.getValue());
} }
} }
@@ -461,7 +486,17 @@ public class MappingMongoConverter implements MongoConverter, ApplicationContext
return Arrays.asList(items); return Arrays.asList(items);
} }
// It's a complex object, have to read it in // It's a complex object, have to read it in
if (dbo.containsField("_class")) {
try {
Class<?> clazz = Class.forName(dbo.get("_class").toString());
dbo.removeField("_class");
o = read(clazz, (DBObject) dbObj);
} catch (ClassNotFoundException e) {
throw new MappingException(e.getMessage(), e);
}
} else {
o = read(type, (DBObject) dbObj); o = read(type, (DBObject) dbObj);
}
} else { } else {
o = dbObj; o = dbObj;
} }
@@ -521,4 +556,22 @@ public class MappingMongoConverter implements MongoConverter, ApplicationContext
} }
} }
protected class PersistentPropertyWrapper<T> {
private final PersistentProperty<T> property;
private final DBObject target;
public PersistentPropertyWrapper(PersistentProperty<T> property, DBObject target) {
this.property = property;
this.target = target;
}
public PersistentProperty<T> getProperty() {
return property;
}
public DBObject getTarget() {
return target;
}
}
} }

View File

@@ -59,9 +59,8 @@ public class MongoMappingConfigurationBuilder extends BasicMappingConfigurationB
} }
@Override @Override
public PersistentProperty<?> createPersistentProperty(Field field, PropertyDescriptor descriptor) throws MappingConfigurationException { public <T> PersistentProperty<T> createPersistentProperty(Field field, PropertyDescriptor descriptor, Class<T> type) throws MappingConfigurationException {
@SuppressWarnings({"unchecked", "rawtypes"}) PersistentProperty<T> property = new MongoPersistentProperty<T>(field.getName(), type, field, descriptor);
PersistentProperty<?> property = new MongoPersistentProperty(field.getName(), field.getType(), field, descriptor);
if (field.isAnnotationPresent(Indexed.class)) { if (field.isAnnotationPresent(Indexed.class)) {
Indexed index = field.getAnnotation(Indexed.class); Indexed index = field.getAnnotation(Indexed.class);
String collection = index.collection(); String collection = index.collection();

View File

@@ -16,10 +16,13 @@
package org.springframework.data.document.mongodb.mapping; package org.springframework.data.document.mongodb.mapping;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.document.mongodb.MongoTemplate; import org.springframework.data.document.mongodb.MongoTemplate;
import org.springframework.data.document.mongodb.convert.MappingMongoConverter;
import org.springframework.data.document.mongodb.query.Criteria; import org.springframework.data.document.mongodb.query.Criteria;
import org.springframework.data.document.mongodb.query.Query; import org.springframework.data.document.mongodb.query.Query;
import org.springframework.data.mapping.BasicMappingContext; import org.springframework.data.mapping.BasicMappingContext;
@@ -33,8 +36,7 @@ import java.util.Map;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.*;
import static org.junit.Assert.assertThat;
/** /**
* @author Jon Brisbin <jbrisbin@vmware.com> * @author Jon Brisbin <jbrisbin@vmware.com>
@@ -47,6 +49,8 @@ public class MappingTests {
MongoTemplate template; MongoTemplate template;
@Autowired @Autowired
BasicMappingContext mappingContext; BasicMappingContext mappingContext;
@Autowired
MappingMongoConverter mongoConverter;
@Test @Test
public void setUp() { public void setUp() {
@@ -54,12 +58,28 @@ public class MappingTests {
template.dropCollection("account"); template.dropCollection("account");
} }
@Test
public void testConvertSimpleProperty() {
PersonPojo p = new PersonPojo(1234, "Person", "Pojo");
DBObject dbo = new BasicDBObject();
mongoConverter.write(p, dbo);
assertEquals(dbo.get("ssn"), 1234);
PersonPojo p2 = mongoConverter.read(PersonPojo.class, dbo);
assertEquals(p.getFirstName(), p2.getFirstName());
}
@Test @Test
public void testPersonPojo() { public void testPersonPojo() {
PersonPojo p = new PersonPojo(12345, "Person", "Pojo"); PersonPojo p = new PersonPojo(12345, "Person", "Pojo");
template.insert(p); template.insert(p);
assertNotNull(p.getId()); assertNotNull(p.getId());
List<PersonPojo> result = template.find(new Query(Criteria.where("_id").is(p.getId())), PersonPojo.class);
assertThat(result.size(), is(1));
assertThat(result.get(0).getSsn(), is(12345));
} }
@Test @Test
@@ -69,6 +89,7 @@ public class MappingTests {
List<PersonCustomIdName> result = template.find(new Query(Criteria.where("ssn").is(123456)), PersonCustomIdName.class); List<PersonCustomIdName> result = template.find(new Query(Criteria.where("ssn").is(123456)), PersonCustomIdName.class);
assertThat(result.size(), is(1)); assertThat(result.size(), is(1));
assertNotNull(result.get(0).getCustomId());
} }
@Test @Test
@@ -87,9 +108,9 @@ public class MappingTests {
assertNotNull(p.getId()); assertNotNull(p.getId());
List<PersonMapProperty> result = template.find(new Query(Criteria.where("ssn").is(1234567)), PersonMapProperty.class); List<PersonMapProperty> result = template.find(new Query(Criteria.where("ssn").is(1234567)), PersonMapProperty.class);
assertThat(result.size(), is(1)); //assertThat(result.size(), is(1));
assertThat(result.get(0).getAccounts().size(), is(2)); assertThat(result.get(0).getAccounts().size(), is(2));
assertNotNull(result.get(0).getAccounts().get("checking")); assertThat(result.get(0).getAccounts().get("checking").getBalance(), is(1000.0f));
} }
@Test @Test

View File

@@ -36,4 +36,5 @@ public class PersonPojo extends BasePerson {
public void setId(ObjectId id) { public void setId(ObjectId id) {
this.id = id; this.id = id;
} }
} }

View File

@@ -0,0 +1,40 @@
/*
* Copyright (c) 2011 by the original author(s).
*
* 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.document.mongodb.mapping;
import java.util.List;
/**
* @author Jon Brisbin <jbrisbin@vmware.com>
*/
public class PersonSimpleList extends BasePerson {
private List<String> nicknames;
public PersonSimpleList(Integer ssn, String firstName, String lastName) {
super(ssn, firstName, lastName);
}
public List<String> getNicknames() {
return nicknames;
}
public void setNicknames(List<String> nicknames) {
this.nicknames = nicknames;
}
}