Search in sources :

Example 41 with MappedClass

use of org.mongodb.morphia.mapping.MappedClass in project morphia by mongodb.

the class IndexHelperTest method weightsOnNonTextIndex.

@Test(expected = MappingException.class)
public void weightsOnNonTextIndex() {
    MongoCollection<Document> indexes = getDatabase().getCollection("indexes");
    MappedClass mappedClass = getMorphia().getMapper().getMappedClass(IndexedClass.class);
    IndexBuilder index = new IndexBuilder().fields(new FieldBuilder().value("name").weight(10));
    indexHelper.createIndex(indexes, mappedClass, index, false);
}
Also used : MappedClass(org.mongodb.morphia.mapping.MappedClass) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) Test(org.junit.Test)

Example 42 with MappedClass

use of org.mongodb.morphia.mapping.MappedClass in project morphia by mongodb.

the class ContainsEmbeddedWithId method checkRecursivelyHasNoIdAnnotationPresent.

private void checkRecursivelyHasNoIdAnnotationPresent(final Set<Class<?>> classesToInspect, final HashSet<Class<?>> alreadyInspectedClasses, final MappedClass mc, final Set<ConstraintViolation> ve) {
    for (final Class<?> clazz : classesToInspect) {
        if (alreadyInspectedClasses.contains(clazz)) {
            continue;
        }
        if (hasTypeFieldAnnotation(clazz, Id.class)) {
            ve.add(new ConstraintViolation(Level.FATAL, mc, getClass(), "You cannot use @Id on any field of an Embedded/Property object"));
        }
        alreadyInspectedClasses.add(clazz);
        final Set<Class<?>> extraClassesToInspect = new HashSet<Class<?>>();
        for (final Field field : ReflectionUtils.getDeclaredAndInheritedFields(clazz, true)) {
            if (isFieldToInspect(field)) {
                extraClassesToInspect.add(field.getType());
            }
        }
        checkRecursivelyHasNoIdAnnotationPresent(extraClassesToInspect, alreadyInspectedClasses, mc, ve);
    }
}
Also used : Field(java.lang.reflect.Field) ConstraintViolation(org.mongodb.morphia.mapping.validation.ConstraintViolation) MappedClass(org.mongodb.morphia.mapping.MappedClass) HashSet(java.util.HashSet)

Example 43 with MappedClass

use of org.mongodb.morphia.mapping.MappedClass in project morphia by mongodb.

the class IndexHelperTest method calculateKeys.

@Test
public void calculateKeys() {
    MappedClass mappedClass = getMorphia().getMapper().getMappedClass(IndexedClass.class);
    BsonDocument keys = indexHelper.calculateKeys(mappedClass, new IndexBuilder().fields(new FieldBuilder().value("text").type(IndexType.TEXT).weight(1), new FieldBuilder().value("nest").type(IndexType.DESC)));
    assertEquals(new BsonDocument().append("text", new BsonString("text")).append("nest", new BsonInt32(-1)), keys);
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) MappedClass(org.mongodb.morphia.mapping.MappedClass) Test(org.junit.Test)

Example 44 with MappedClass

use of org.mongodb.morphia.mapping.MappedClass in project morphia by mongodb.

the class IndexHelperTest method wildcardTextIndex.

@Test
public void wildcardTextIndex() {
    MongoCollection<Document> indexes = getDatabase().getCollection("indexes");
    MappedClass mappedClass = getMorphia().getMapper().getMappedClass(IndexedClass.class);
    IndexBuilder index = new IndexBuilder().fields(new FieldBuilder().value("$**").type(IndexType.TEXT));
    indexHelper.createIndex(indexes, mappedClass, index, false);
    List<DBObject> wildcard = getDb().getCollection("indexes").getIndexInfo();
    boolean found = false;
    for (DBObject dbObject : wildcard) {
        found |= dbObject.get("name").equals("$**_text");
    }
    assertTrue("Should have found the wildcard index", found);
}
Also used : MappedClass(org.mongodb.morphia.mapping.MappedClass) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Test(org.junit.Test)

Example 45 with MappedClass

use of org.mongodb.morphia.mapping.MappedClass in project morphia by mongodb.

the class IndexHelperTest method textPartialFilters.

@Test
public void textPartialFilters() {
    MongoCollection<Document> collection = getDatabase().getCollection("indexes");
    MappedClass mappedClass = getMorphia().getMapper().getMappedClass(IndexedClass.class);
    Text text = new TextBuilder().value(4).options(new IndexOptionsBuilder().partialFilter("{ name : { $gt : 13 } }"));
    indexHelper.createIndex(collection, mappedClass, indexHelper.convert(text, "text"), false);
    findPartialIndex(BasicDBObject.parse(text.options().partialFilter()));
}
Also used : Text(org.mongodb.morphia.annotations.Text) MappedClass(org.mongodb.morphia.mapping.MappedClass) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) Test(org.junit.Test)

Aggregations

MappedClass (org.mongodb.morphia.mapping.MappedClass)92 Test (org.junit.Test)73 MappedField (org.mongodb.morphia.mapping.MappedField)56 Mapper (org.mongodb.morphia.mapping.Mapper)53 ArrayList (java.util.ArrayList)48 BasicDBObject (com.mongodb.BasicDBObject)18 DBObject (com.mongodb.DBObject)12 BsonDocument (org.bson.BsonDocument)8 Document (org.bson.Document)8 MappingException (org.mongodb.morphia.mapping.MappingException)6 DBCollection (com.mongodb.DBCollection)5 List (java.util.List)5 SimpleEntity (org.mongodb.morphia.entities.SimpleEntity)5 ObjectId (org.bson.types.ObjectId)4 Key (org.mongodb.morphia.Key)4 Index (org.mongodb.morphia.annotations.Index)4 LinkedHashMap (java.util.LinkedHashMap)3 NotSaved (org.mongodb.morphia.annotations.NotSaved)3 UpdateResults (org.mongodb.morphia.query.UpdateResults)3 WriteResult (com.mongodb.WriteResult)2