diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/convert/MappingMongoConverter.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/convert/MappingMongoConverter.java index be66de1ee..2668d6957 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/convert/MappingMongoConverter.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/convert/MappingMongoConverter.java @@ -413,7 +413,12 @@ public class MappingMongoConverter implements MongoConverter, ApplicationContext Class type = prop.getType(); if (prop.isCollection()) { BasicDBList dbList = new BasicDBList(); - Collection coll = (type.isArray() ? Arrays.asList((Object[]) obj) : (Collection) obj); + Collection coll; + if (type.isArray()) { + coll = Arrays.asList((Object[]) obj); + } else { + coll = (Collection) obj; + } for (Object propObjItem : coll) { if (null != dbref) { DBRef dbRef = createDBRef(propObjItem, dbref); @@ -569,7 +574,7 @@ public class MappingMongoConverter implements MongoConverter, ApplicationContext } return Arrays.asList(items); } - + Class toType = findTypeToBeUsed((DBObject) dbObj); // It's a complex object, have to read it in @@ -594,11 +599,11 @@ public class MappingMongoConverter implements MongoConverter, ApplicationContext */ protected Class findTypeToBeUsed(DBObject dbObject) { Object classToBeUsed = dbObject.get(CUSTOM_TYPE_KEY); - + if (classToBeUsed == null) { return null; } - + try { return Class.forName(classToBeUsed.toString()); } catch (ClassNotFoundException e) { diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/mapping/MongoMappingContext.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/mapping/MongoMappingContext.java index decccca73..4954aebae 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/mapping/MongoMappingContext.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/mapping/MongoMappingContext.java @@ -46,6 +46,7 @@ public class MongoMappingContext extends BasicMappingContext { simpleTypes.add(com.mongodb.DBRef.class); simpleTypes.add(ObjectId.class); simpleTypes.add(CodeWScope.class); + simpleTypes.add(Character.class); } @Override diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/Location.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/Location.java new file mode 100644 index 000000000..6949b9c54 --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/Location.java @@ -0,0 +1,65 @@ +/* + * 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 org.bson.types.ObjectId; + +/** + * @author Jon Brisbin + */ +@Document +public class Location { + + private ObjectId id; + private double[] latlong; + private int[] numbers; + private float[] amounts; + + public Location(double[] latlong, int[] numbers, float[] amounts) { + this.latlong = latlong; + this.numbers = numbers; + this.amounts = amounts; + } + + public ObjectId getId() { + return id; + } + + public double[] getLatlong() { + return latlong; + } + + public void setLatlong(double[] latlong) { + this.latlong = latlong; + } + + public int[] getNumbers() { + return numbers; + } + + public void setNumbers(int[] numbers) { + this.numbers = numbers; + } + + public float[] getAmounts() { + return amounts; + } + + public void setAmounts(float[] amounts) { + this.amounts = amounts; + } +} diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/MappingTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/MappingTests.java index d92d40469..783b705a9 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/MappingTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/MappingTests.java @@ -162,4 +162,17 @@ public class MappingTests { assertThat(result.size(), is(1)); } + @Test + public void testPrimitives() { + Location loc = new Location( + new double[]{1.0, 2.0}, + new int[]{1, 2, 3, 4}, + new float[]{1.0f, 2.0f} + ); + template.insert("locations", loc); + + List result = template.find("locations", new Query(Criteria.where("_id").is(loc.getId())), Location.class); + assertThat(result.size(), is(1)); + } + } diff --git a/src/docbkx/index.xml b/src/docbkx/index.xml index d6b54a47c..05c4bb800 100644 --- a/src/docbkx/index.xml +++ b/src/docbkx/index.xml @@ -26,6 +26,10 @@ Costin Leau + + Jon + Brisbin + @@ -57,6 +61,7 @@ + diff --git a/src/docbkx/reference/mapping.xml b/src/docbkx/reference/mapping.xml new file mode 100644 index 000000000..b815dc065 --- /dev/null +++ b/src/docbkx/reference/mapping.xml @@ -0,0 +1,178 @@ + + + + Mapping support + + The object mapping support for MongoDB + + +
+ MongoDB Mapping Configuration + + Spring's Mongo namespace enables you to easily enable mapping functionality + + + XML schema to configure MongoDB mapping support + + + + + + + + + + + + + + + + + This sets up the right objects in the ApplicationContext to perform the full gamut + of mapping operations. The base-package property tells it where to scan for + classes annotated with the @org.springframework.data.document.mongodb.mapping.Document + annotation and the autowire property tells it whether to pass mapped domain objects through the + Spring ApplicationContext's autowiring mechanism, allowing you to use + @org.springframework.beans.factory.annotation.Autowired inside your domain objects. + + +
+ +
+ Mapping Framework Usage + + To take full advantage of the object mapping functionality inside the Spring Data/MongoDB support, + you should annotate your mapped objects with the @org.springframework.data.document.mongodb.mapping.Document + annotation. Although it is not necessary for the mapping framework to have this annotation (your POJOs + will be mapped correctly, even without any annotations), it allows the classpath scanner to find and + pre-process your domain objects to extract the necessary metadata. If you don't use this annotation, + your application will take a slight performance hit the first time you store a domain object because the + mapping framework needs to build up its internal metadata model so it knows about the properties of your + domain object and how to persist them. + + + + Example domain object + + + + + + + The @Id annotation tells the mapper which property you want to use for the + MongoDB _id property and the @Indexed annotation tells the mapping + framework to call ensureIndex on that property of your document, making searches faster. + + + +
+ Compound Indexes + + Compound indexes are also supported. They are defined at the class level, rather than on indidvidual + properties. Here's an example that creates a compound index of lastName in ascending order + and age in descending order: + + + Example Compound Index Usage + + + + +
+ +
+ Using DBRefs + + The mapping framework doesn't have to store child objects embedded within the document. You can also + store them separately and use a DBRef to refer to that document. When the object is loaded from MongoDB, + those references will be eagerly resolved and you will get back a mapped object that looks the same as if + it had been stored embedded within your master document. + + + Here's an example of using a DBRef to refer to a specific document that exists independently of the + object in which it is referenced (both classes are shown in-line for brevity's sake): + + + + Child object referred to using a DBRef + + accounts; + +}]]> + + + + There's no need to use something like @OneToMany because the mapping framework sees that + you're wanting a one-to-many relationship because there is a List of objects. When the object is stored + in MongoDB, there will be a list of DBRefs rather than the Account objects themselves. + + + The mapping framework does not handle cascading saves. If you change an Account object that is + referenced by a Person object, you must save the Account object separately. Calling save + on the Person object will not automatically save the Account objects in the + property accounts. + + +
+ +
+ Handling Mapping Framework Events + + Built into the MongoDB mapping framework are several org.springframework.context.ApplicationEvent + events that your application can respond to + +
+
+