Package org.mongodb.morphia.mapping

Examples of org.mongodb.morphia.mapping.MappedClass


        return savedKeys;
    }

    private <T> DBObject toDbObject(final T ent, final Map<Object, DBObject> involvedObjects) {
        final MappedClass mc = mapper.getMappedClass(ent);
        if (mc.getAnnotation(NotSaved.class) != null) {
            throw new MappingException(String.format("Entity type: %s is marked as NotSaved which means you should not try to save it!",
                                                     mc.getClazz().getName()));
        }
        DBObject dbObject = entityToDBObj(ent, involvedObjects);
        List<MappedField> versionFields = mc.getFieldsAnnotatedWith(Version.class);
        for (MappedField mappedField : versionFields) {
            String name = mappedField.getNameToStore();
            if (dbObject.get(name) == null) {
                dbObject.put(name, 1);
                mappedField.setFieldValue(ent, 1L);
View Full Code Here


    protected <T> Key<T> save(final DBCollection dbColl, final T entity, final WriteConcern wc) {
        if (entity == null) {
            throw new UpdateException("Can not persist a null entity");
        }

        final MappedClass mc = mapper.getMappedClass(entity);
        if (mc.getAnnotation(NotSaved.class) != null) {
            throw new MappingException(
                                          "Entity type: " + mc.getClazz().getName()
                                          + " is marked as NotSaved which means you should not try to save it!"
            );
        }

        WriteResult wr;
View Full Code Here

    public <T> UpdateResults update(final T ent, final UpdateOperations<T> ops) {
        if (ent instanceof Query) {
            return update((Query<T>) ent, ops);
        }

        final MappedClass mc = mapper.getMappedClass(ent);
        final Query<T> q = (Query<T>) createQuery(mc.getClazz());
        q.disableValidation().filter(Mapper.ID_KEY, mapper.getId(ent));

        if (!mc.getFieldsAnnotatedWith(Version.class).isEmpty()) {
            final MappedField versionMF = mc.getFieldsAnnotatedWith(Version.class).get(0);
            final Long oldVer = (Long) versionMF.getFieldValue(ent);
            q.filter(versionMF.getNameToStore(), oldVer);
            ops.set(versionMF.getNameToStore(), nextValue(oldVer));
        }
View Full Code Here

        final Object idValue = dbObj.get(Mapper.ID_KEY);
        dbObj.removeField(Mapper.ID_KEY);

        WriteResult wr;

        final MappedClass mc = mapper.getMappedClass(unwrapped);
        final DBCollection dbColl = getCollection(unwrapped);

        //try to do an update if there is a @Version field
        wr = tryVersionedUpdate(dbColl, unwrapped, dbObj, idValue, wc, mc);
View Full Code Here

    private void postSaveOperations(final Map<Object, DBObject> involvedObjects) {
        for (final Map.Entry<Object, DBObject> e : involvedObjects.entrySet()) {
            final Object ent = e.getKey();
            final DBObject dbO = e.getValue();
            final MappedClass mc = mapper.getMappedClass(ent);
            mc.callLifecycleMethods(PostPersist.class, ent, dbO, mapper);
        }
    }
View Full Code Here

        public void preLoad(final Object ent, final DBObject dbObj, final Mapper mapper) {
        }

        public void prePersist(final Object ent, final DBObject dbObj, final Mapper mapper) {
            final MappedClass mc = mapper.getMappedClass(ent);
            final List<MappedField> toLowercase = mc.getFieldsAnnotatedWith(Lowercase.class);
            for (final MappedField mf : toLowercase) {
                try {
                    final Object fieldValue = mf.getFieldValue(ent);
                    dbObj.put(mf.getNameToStore() + "_lowercase", fieldValue.toString().toLowerCase());
                } catch (Exception e) {
View Full Code Here

    @Test
    public void testExternalMapping() throws Exception {
        final Mapper mapper = getMorphia().getMapper();
        final CloneMapper helper = new CloneMapper(mapper);
        helper.map(Skeleton.class, EntityWithNoAnnotations.class);
        final MappedClass mc = mapper.getMappedClass(EntityWithNoAnnotations.class);
        mc.update();
        assertNotNull(mc.getIdField());
        assertNotNull(mc.getEntityAnnotation());
        assertEquals("special", mc.getEntityAnnotation().value());

        EntityWithNoAnnotations ent = new EntityWithNoAnnotations();
        ent.id = "test";
        final Key<EntityWithNoAnnotations> k = getDs().save(ent);
        assertNotNull(k);
View Full Code Here

        public CloneMapper(final Mapper mapper) {
            this.mapper = mapper;
        }

        void map(final Class sourceClass, final Class destClass) {
            final MappedClass destMC = mapper.getMappedClass(destClass);
            final MappedClass sourceMC = mapper.getMappedClass(sourceClass);
            //copy the class level annotations
            for (final Entry<Class<? extends Annotation>, List<Annotation>> e : sourceMC.getRelevantAnnotations().entrySet()) {
                if (e.getValue() != null && !e.getValue().isEmpty()) {
                    for (final Annotation ann : e.getValue()) {
                        destMC.addAnnotation(e.getKey(), ann);
                    }
                }
            }
            //copy the fields.
            for (final MappedField mf : sourceMC.getPersistenceFields()) {
                final Map<Class<? extends Annotation>, Annotation> annMap = mf.getAnnotations();
                final MappedField destMF = destMC.getMappedFieldByJavaField(mf.getJavaFieldName());
                if (destMF != null && annMap != null && !annMap.isEmpty()) {
                    for (final Entry<Class<? extends Annotation>, Annotation> e : annMap.entrySet()) {
                        destMF.addAnnotation(e.getKey(), e.getValue());
View Full Code Here

public class GeoWithinOperationValidatorTest {
    @Test
    public void shouldAllowGeoWithinOperatorWithAllAppropriateTrimmings() {
        // given
        List<ValidationFailure> validationFailures = new ArrayList<ValidationFailure>();
        MappedClass mappedClass = new MappedClass(GeoEntity.class, new Mapper());
        MappedField mappedField = mappedClass.getMappedField("array");

        // when
        assertThat(GeoWithinOperationValidator.getInstance().apply(mappedField, GEO_WITHIN, new BasicDBObject("$box", 1),
                                                                   validationFailures), is(true));
    }
View Full Code Here

    @Test
    public void shouldAllowGeoWithinOperatorForGeoEntityWithListOfIntegers() {
        // given
        List<ValidationFailure> validationFailures = new ArrayList<ValidationFailure>();
        MappedClass mappedClass = new MappedClass(GeoEntity.class, new Mapper());
        MappedField mappedField = mappedClass.getMappedField("list");
        assertThat(GeoWithinOperationValidator.getInstance().apply(mappedField,
                                                                   GEO_WITHIN,
                                                                   new BasicDBObject("$box", 1),
                                                                   validationFailures),
                   is(true)
View Full Code Here

TOP

Related Classes of org.mongodb.morphia.mapping.MappedClass

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.