@Autowired @SuppressWarnings("unchecked") public void setMongo(Mongo mongo) throws Exception { CustomConversions conversions = new CustomConversions( Arrays.asList(DateToDateTimeConverter.INSTANCE, DateTimeToDateConverter.INSTANCE)); MongoMappingContext mappingContext = new MongoMappingContext(); mappingContext.setInitialEntitySet( new HashSet<Class<?>>( Arrays.asList( PersonWith_idPropertyOfTypeObjectId.class, PersonWith_idPropertyOfTypeString.class, PersonWithIdPropertyOfTypeObjectId.class, PersonWithIdPropertyOfTypeString.class, PersonWithIdPropertyOfTypeInteger.class, PersonWithIdPropertyOfPrimitiveInt.class, PersonWithIdPropertyOfTypeLong.class, PersonWithIdPropertyOfPrimitiveLong.class))); mappingContext.setSimpleTypeHolder(conversions.getSimpleTypeHolder()); mappingContext.afterPropertiesSet(); MappingMongoConverter mappingConverter = new MappingMongoConverter(factory, mappingContext); mappingConverter.setCustomConversions(conversions); mappingConverter.afterPropertiesSet(); this.mappingTemplate = new MongoTemplate(factory, mappingConverter); }
@Bean public MongoConverter mongoConverter() throws Exception { MongoMappingContext mappingContext = new MongoMappingContext(); MappingMongoConverter mappingMongoConverter = new MappingMongoConverter(mongoDbFactory(), mappingContext); mappingMongoConverter.afterPropertiesSet(); return mappingMongoConverter; }
@Bean @Override public MappingMongoConverter mappingMongoConverter() throws Exception { MappingMongoConverter mmc = super.mappingMongoConverter(); // Removing _class from the persisted entity mmc.setTypeMapper(new DefaultMongoTypeMapper(null)); return mmc; }
@Bean @DependsOn("customConversions") @Autowired public MappingMongoConverter mongoConverter() throws Exception { MongoMappingContext mappingContext = new MongoMappingContext(); DbRefResolver dbRefResolver = new DefaultDbRefResolver(factory); MappingMongoConverter mongoConverter = new MappingMongoConverter(dbRefResolver, mappingContext); mongoConverter.setCustomConversions(customConversions()); mongoConverter.afterPropertiesSet(); return mongoConverter; }
@Autowired @SuppressWarnings("unchecked") public void setMongo(Mongo mongo) throws Exception { MongoMappingContext mappingContext = new MongoMappingContext(); mappingContext.setInitialEntitySet(new HashSet<Class<?>>(Arrays.asList(ValueObject.class))); mappingContext.afterPropertiesSet(); MappingMongoConverter mappingConverter = new MappingMongoConverter(factory, mappingContext); mappingConverter.afterPropertiesSet(); this.mongoTemplate = new MongoTemplate(factory, mappingConverter); }
@SuppressWarnings("deprecation") public void beforeLocalFile() throws NumberFormatException, FileNotFoundException, IOException { readConfig(); List<ServerAddress> address = new LinkedList<ServerAddress>(); for (String url : mongoDb.split(",")) { String[] addr = url.split(":"); if (addr.length != 2) { throw new IllegalArgumentException("wrong mongo db address:" + mongoDb); } ServerAddress sa = new ServerAddress(addr[0], Integer.parseInt(addr[1])); address.add(sa); } mongoTemplate = new MongoTemplate(new Mongo(address), dataBase); mongoTemplate.setWriteResultChecking(WriteResultChecking.EXCEPTION); if (mongoTemplate.getConverter() instanceof MappingMongoConverter) { MappingMongoConverter converter = (MappingMongoConverter) mongoTemplate.getConverter(); converter.setMapKeyDotReplacement(MongoDbStartInitializer.MAP_KEY_DOT_REPLACEMENT); } }
/** @see DATAMONGO-863 */ @Test public void doesNotConvertRawDbObjects() { Update update = new Update(); update.pull( "options", new BasicDBObject( "_id", new BasicDBObject("$in", converter.convertToMongoType(Arrays.asList(1L, 2L))))); DBObject mappedObject = mapper.getMappedObject( update.getUpdateObject(), context.getPersistentEntity(ParentClass.class)); DBObject setClause = getAsDBObject(mappedObject, "$pull"); DBObject options = getAsDBObject(setClause, "options"); DBObject idClause = getAsDBObject(options, "_id"); BasicDBList inClause = getAsDBList(idClause, "$in"); assertThat(inClause, IsIterableContainingInOrder.<Object>contains(1L, 2L)); }
private String fetchBatches( DBCollection collection, Set<String> loadedUris, String lastDCIdFetched) { BulkWriteOperation builder = collection.initializeUnorderedBulkOperation(); String prevDcId = lastDCIdFetched; DCQueryParameters params; params = lastDCIdFetched == null ? new DCQueryParameters(searchCronField, DCOrdering.DESCENDING) : new DCQueryParameters( searchCronField, DCOrdering.DESCENDING, DCOperator.LT, "\"" + lastDCIdFetched + "\""); // (LT & descending order to leave possible null geo:name values at the end rather than having // to skip them) logger.debug("Querying the Data Core"); long queryStart = System.currentTimeMillis(); List<DCResource> resources = datacore.findResources(project, areaModel, params, 0, batchSize); long queryEnd = System.currentTimeMillis(); logger.debug("Fetched {} resources in {} ms", resources.size(), queryEnd - queryStart); boolean hasOne = false; for (DCResource res : resources) { String name = null; for (Languages language : Languages.values()) { GeographicalArea area = geographicalDAO.toGeographicalArea(res, language.getLanguage()); if (area == null) { continue; } if (!loadedUris.contains(language.getLanguage() + area.getUri())) { hasOne = true; if (name == null) { name = area.getName(); // logger.debug("{} - {}", name, area.getUri()); } DBObject dbObject = new BasicDBObject(); mappingMongoConverter.write(area, dbObject); builder.insert(dbObject); loadedUris.add(language.getLanguage() + area.getUri()); } else { if (name == null) { name = area.getName(); logger.debug( "Area {} already inserted for language {}, skipping", area.getName(), language.getLanguage()); } } } String id = res .getUri(); // ID resource in DC is always encoded, so to match values we need to // encoded as well if (id != null) { lastDCIdFetched = id; } } if (hasOne) { long st = System.currentTimeMillis(); builder.execute(); long durationSave = System.currentTimeMillis() - st; logger.debug( "Saved resources; total save time={} ms (avg = {} ms)", durationSave, durationSave / resources.size()); } if ((prevDcId != null && prevDcId.equals(lastDCIdFetched)) || resources.size() < batchSize) { return null; } else return lastDCIdFetched; }