Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright 2013 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
  * 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.
 import java.util.Map;
 import  org.springframework.util.Assert;
Wrapper value object for a BasicDBObject to be able to access raw values by MongoPersistentProperty references. The accessors will transparently resolve nested document values that a MongoPersistentProperty might refer to through a path expression in field names.

Oliver Gierke
 class DBObjectAccessor {
 	private final DBObject dbObject;

Creates a new DBObjectAccessor for the given DBObject.

dbObject must be a BasicDBObject effectively, must not be null.
 	public DBObjectAccessor(DBObject dbObject) {
 		Assert.notNull(dbObject"DBObject must not be null!");
 		Assert.isInstanceOf(BasicDBObject.classdbObject"Given DBObject must be a BasicDBObject!");
 		this. = dbObject;

Puts the given value into the backing DBObject based on the coordinates defined through the given MongoPersistentProperty. By default this will be the plain field name. But field names might also consist of path traversals so we might need to create intermediate BasicDBObjects.

prop must not be null.
 	public void put(MongoPersistentProperty propObject value) {
 		Assert.notNull(prop"MongoPersistentProperty must not be null!");
 		String fieldName = prop.getFieldName();
 		Iterator<Stringparts = Arrays.asList(fieldName.split("\\.")).iterator();
 		DBObject dbObject = this.;
 		while (parts.hasNext()) {
 			String part =;
 			if (parts.hasNext()) {
 				BasicDBObject nestedDbObject = new BasicDBObject();
 				dbObject = nestedDbObject;
 			} else {

Returns the value the given MongoPersistentProperty refers to. By default this will be a direct field but the method will also transparently resolve nested values the MongoPersistentProperty might refer to through a path expression in the field name metadata.

property must not be null.
 	public Object get(MongoPersistentProperty property) {
 		String fieldName = property.getFieldName();
 		Iterator<Stringparts = Arrays.asList(fieldName.split("\\.")).iterator();
 		Map<ObjectObjectsource = this..toMap();
 		Object result = null;
 		while (source != null && parts.hasNext()) {
			result = source.get(;
			if (parts.hasNext()) {
				source = getAsMap(result);
		return result;
	private Map<ObjectObjectgetAsMap(Object source) {
		if (source instanceof BasicDBObject) {
			return ((DBObjectsource).toMap();
		if (source instanceof Map) {
			return (Map<ObjectObject>) source;
		return null;
New to GrepCode? Check out our FAQ X