Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2011-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
   *
   *      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.mapping;
 
 import java.util.Map;
 
 import  org.springframework.beans.BeansException;
 import  org.springframework.context.ApplicationContext;
 import  org.springframework.context.ApplicationContextAware;
 import  org.springframework.context.expression.BeanFactoryAccessor;
 import  org.springframework.context.expression.BeanFactoryResolver;
 import  org.springframework.expression.Expression;
 import  org.springframework.expression.ParserContext;
 import  org.springframework.expression.spel.standard.SpelExpressionParser;
 import  org.springframework.expression.spel.support.StandardEvaluationContext;
 import  org.springframework.util.Assert;
 import  org.springframework.util.StringUtils;

MongoDB specific MongoPersistentEntity implementation that adds Mongo specific meta-data such as the collection name and the like.

Author(s):
Jon Brisbin
Oliver Gierke
Thomas Darimont
 
 public class BasicMongoPersistentEntity<T> extends BasicPersistentEntity<T, MongoPersistentPropertyimplements
 		MongoPersistentEntity<T>, ApplicationContextAware {
 
 	private static final String AMBIGUOUS_FIELD_MAPPING = "Ambiguous field mapping detected! Both %s and %s map to the same field name %s! Disambiguate using @Field annotation!";
 	private final String collection;
 	private final SpelExpressionParser parser;
 	private final StandardEvaluationContext context;

Creates a new BasicMongoPersistentEntity with the given TypeInformation. Will default the collection name to the entities simple type name.

Parameters:
typeInformation
 
 	public BasicMongoPersistentEntity(TypeInformation<T> typeInformation) {
 
 		super(typeInformation.);
 
 		this. = new SpelExpressionParser();
 		this. = new StandardEvaluationContext();
 
 		Class<?> rawType = typeInformation.getType();
 		String fallback = MongoCollectionUtils.getPreferredCollectionName(rawType);
 
 		if (rawType.isAnnotationPresent(Document.class)) {
 			Document d = rawType.getAnnotation(Document.class);
 			this. = StringUtils.hasText(d.collection()) ? d.collection() : fallback;
 		} else {
 			this. = fallback;
 		}
 	}
 
 	/*
 	 * (non-Javadoc)
 	 * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
 	 */
 	public void setApplicationContext(ApplicationContext applicationContextthrows BeansException {
 
 		.addPropertyAccessor(new BeanFactoryAccessor());
 		.setBeanResolver(new BeanFactoryResolver(applicationContext));
 		.setRootObject(applicationContext);
 	}
 
 	/*
 	 * (non-Javadoc)
 	 * @see org.springframework.data.mongodb.core.mapping.MongoPersistentEntity#getCollection()
 	 */
 	public String getCollection() {
 		Expression expression = .parseExpression(, ParserContext.TEMPLATE_EXPRESSION);
		return expression.getValue(String.class);
	}
	/* 
	 * (non-Javadoc)
	 * @see org.springframework.data.mapping.model.BasicPersistentEntity#verify()
	 */
	public void verify() {
	}

Comparator implementation inspecting the MongoPersistentProperty's order.

Author(s):
Oliver Gierke
		INSTANCE;
		/*
		 * (non-Javadoc)
		 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
		 */
			if (o1.getFieldOrder() == .) {
				return 1;
			}
			if (o2.getFieldOrder() == .) {
				return -1;
			}
			return o1.getFieldOrder() - o2.getFieldOrder();
		}
	}

As a general note: An implicit id property has a name that matches "id" or "_id". An explicit id property is one that is annotated with

Parameters:
property - the new id property candidate
Returns:
See also:
Id. The property id is updated according to the following rules: 1) An id property which is defined explicitly takes precedence over an implicitly defined id property. 2) In case of any ambiguity a @see MappingException is thrown.
		Assert.notNull(property);
		if (!property.isIdProperty()) {
			return null;
		}
		MongoPersistentProperty currentIdProperty = getIdProperty();
		boolean currentIdPropertyIsSet = currentIdProperty != null;
		boolean currentIdPropertyIsExplicit = currentIdPropertyIsSet ? currentIdProperty.isExplicitIdProperty() : false;
		boolean newIdPropertyIsExplicit = property.isExplicitIdProperty();
		if (!currentIdPropertyIsSet) {
			return property;
		}
		Field currentIdPropertyField = currentIdProperty.getField();
		if (newIdPropertyIsExplicit && currentIdPropertyIsExplicit) {
			throw new MappingException(String.format(
					"Attempt to add explicit id property %s but already have an property %s registered "
"as explicit id. Check your mapping configuration!"property.getField(), currentIdPropertyField));
else if (newIdPropertyIsExplicit && !currentIdPropertyIsExplicit) {
			// explicit id property takes precedence over implicit id property
			return property;
else if (!newIdPropertyIsExplicit && currentIdPropertyIsExplicit) {
			// no id property override - current property is explicitly defined
else {
			throw new MappingException(String.format(
					"Attempt to add id property %s but already have an property %s registered "
"as id. Check your mapping configuration!"property.getField(), currentIdPropertyField));
		}
		return null;
	}

Handler to collect MongoPersistentProperty instances and check that each of them is mapped to a distinct field name.

Author(s):
Oliver Gierke
		public void doWithPersistentProperty(MongoPersistentProperty persistentProperty) {
			assertUniqueness(persistentProperty);
		}
		}
		private void assertUniqueness(MongoPersistentProperty property) {
			String fieldName = property.getFieldName();
			MongoPersistentProperty existingProperty = .get(fieldName);
			if (existingProperty != null) {
						existingProperty.toString(), fieldName));
			}
			.put(fieldNameproperty);
		}
	}
New to GrepCode? Check out our FAQ X