Example usage for org.springframework.data.mongodb.core.mapping MongoPersistentEntity getCollection

List of usage examples for org.springframework.data.mongodb.core.mapping MongoPersistentEntity getCollection

Introduction

In this page you can find the example usage for org.springframework.data.mongodb.core.mapping MongoPersistentEntity getCollection.

Prototype

String getCollection();

Source Link

Document

Returns the collection the entity shall be persisted to.

Usage

From source file:org.springframework.data.mongodb.core.convert.MappingMongoConverter.java

protected DBRef createDBRef(Object target, org.springframework.data.mongodb.core.mapping.DBRef dbref) {

    MongoPersistentEntity<?> targetEntity = mappingContext.getPersistentEntity(target.getClass());
    if (null == targetEntity || null == targetEntity.getIdProperty()) {
        return null;
    }//from w  w w .j  a v  a  2 s .  c om

    MongoPersistentProperty idProperty = targetEntity.getIdProperty();
    BeanWrapper<MongoPersistentEntity<Object>, Object> wrapper = BeanWrapper.create(target, conversionService);
    Object id = wrapper.getProperty(idProperty, Object.class, useFieldAccessOnly);

    if (null == id) {
        throw new MappingException("Cannot create a reference to an object with a NULL id.");
    }

    String collection = dbref.collection();
    if ("".equals(collection)) {
        collection = targetEntity.getCollection();
    }

    String dbname = dbref.db();
    DB db = StringUtils.hasText(dbname) ? mongoDbFactory.getDb(dbname) : mongoDbFactory.getDb();

    return new DBRef(db, collection, idMapper.convertId(id));
}

From source file:org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.java

protected void checkForIndexes(final MongoPersistentEntity<?> entity) {
    final Class<?> type = entity.getType();
    if (!classesSeen.containsKey(type)) {
        if (log.isDebugEnabled()) {
            log.debug("Analyzing class " + type + " for index information.");
        }//ww  w .  j a v a2  s  .c o  m

        // Make sure indexes get created
        if (type.isAnnotationPresent(CompoundIndexes.class)) {
            CompoundIndexes indexes = type.getAnnotation(CompoundIndexes.class);
            for (CompoundIndex index : indexes.value()) {

                String indexColl = StringUtils.hasText(index.collection()) ? index.collection()
                        : entity.getCollection();
                DBObject definition = (DBObject) JSON.parse(index.def());

                ensureIndex(indexColl, index.name(), definition, index.unique(), index.dropDups(),
                        index.sparse());

                if (log.isDebugEnabled()) {
                    log.debug("Created compound index " + index);
                }
            }
        }

        entity.doWithProperties(new PropertyHandler<MongoPersistentProperty>() {
            public void doWithPersistentProperty(MongoPersistentProperty persistentProperty) {

                Field field = persistentProperty.getField();

                if (field.isAnnotationPresent(Indexed.class)) {

                    Indexed index = field.getAnnotation(Indexed.class);
                    String name = index.name();

                    if (!StringUtils.hasText(name)) {
                        name = persistentProperty.getFieldName();
                    } else {
                        if (!name.equals(field.getName()) && index.unique() && !index.sparse()) {
                            // Names don't match, and sparse is not true. This situation will generate an error on the server.
                            if (log.isWarnEnabled()) {
                                log.warn("The index name " + name + " doesn't match this property name: "
                                        + field.getName()
                                        + ". Setting sparse=true on this index will prevent errors when inserting documents.");
                            }
                        }
                    }

                    String collection = StringUtils.hasText(index.collection()) ? index.collection()
                            : entity.getCollection();
                    int direction = index.direction() == IndexDirection.ASCENDING ? 1 : -1;
                    DBObject definition = new BasicDBObject(persistentProperty.getFieldName(), direction);

                    ensureIndex(collection, name, definition, index.unique(), index.dropDups(), index.sparse());

                    if (log.isDebugEnabled()) {
                        log.debug("Created property index " + index);
                    }

                } else if (field.isAnnotationPresent(GeoSpatialIndexed.class)) {

                    GeoSpatialIndexed index = field.getAnnotation(GeoSpatialIndexed.class);

                    GeospatialIndex indexObject = new GeospatialIndex(persistentProperty.getFieldName());
                    indexObject.withMin(index.min()).withMax(index.max());
                    indexObject.named(StringUtils.hasText(index.name()) ? index.name() : field.getName());

                    String collection = StringUtils.hasText(index.collection()) ? index.collection()
                            : entity.getCollection();
                    mongoDbFactory.getDb().getCollection(collection).ensureIndex(indexObject.getIndexKeys(),
                            indexObject.getIndexOptions());

                    if (log.isDebugEnabled()) {
                        log.debug(String.format("Created %s for entity %s in collection %s! ", indexObject,
                                entity.getType(), collection));
                    }
                }
            }
        });

        classesSeen.put(type, true);
    }
}

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

public <T> T findById(Object id, Class<T> entityClass) {
    MongoPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(entityClass);
    return findById(id, entityClass, persistentEntity.getCollection());
}

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

protected <T> void doInsertAll(Collection<? extends T> listToSave, MongoWriter<T> writer) {
    Map<String, List<T>> objs = new HashMap<String, List<T>>();

    for (T o : listToSave) {

        MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(o.getClass());
        if (entity == null) {
            throw new InvalidDataAccessApiUsageException(
                    "No Persitent Entity information found for the class " + o.getClass().getName());
        }//from  ww w. j a v a  2s .c o  m
        String collection = entity.getCollection();

        List<T> objList = objs.get(collection);
        if (null == objList) {
            objList = new ArrayList<T>();
            objs.put(collection, objList);
        }
        objList.add(o);

    }

    for (Map.Entry<String, List<T>> entry : objs.entrySet()) {
        doInsertBatch(entry.getKey(), entry.getValue(), this.mongoConverter);
    }
}

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

String determineCollectionName(Class<?> entityClass) {

    if (entityClass == null) {
        throw new InvalidDataAccessApiUsageException(
                "No class parameter provided, entity collection can't be determined!");
    }/* w w w .ja v  a 2s .  c  om*/

    MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass);
    if (entity == null) {
        throw new InvalidDataAccessApiUsageException(
                "No Persitent Entity information found for the class " + entityClass.getName());
    }
    return entity.getCollection();
}

From source file:org.springframework.data.mongodb.repository.support.QuerydslRepositorySupport.java

/**
 * Returns a {@link MongodbQuery} for the given {@link EntityPath}. The collection being queried is derived from the
 * entity metadata./*from w  w  w  . j a  va  2s.  c om*/
 * 
 * @param path
 * @return
 */
protected <T> MongodbQuery<T> from(final EntityPath<T> path) {
    Assert.notNull(path);
    MongoPersistentEntity<?> entity = context.getPersistentEntity(path.getType());
    return from(path, entity.getCollection());
}