From 186caba1ac5ac1f228da4b94eed36fd66fdd23dd Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Tue, 2 Apr 2013 11:42:36 +0200 Subject: [PATCH] DATAMONGO-642 - MongoChangeSetPersister now considers mapped collection. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit So far the change set persister has used the plain domain type name to persist data. We now consider the collection name defined by the object mapping (through @Document(collection = "…")). --- .../crossstore/MongoChangeSetPersister.java | 27 ++++++++++++++++--- .../crossstore/CrossStoreMongoTests.java | 20 ++++++++------ 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/spring-data-mongodb-cross-store/src/main/java/org/springframework/data/mongodb/crossstore/MongoChangeSetPersister.java b/spring-data-mongodb-cross-store/src/main/java/org/springframework/data/mongodb/crossstore/MongoChangeSetPersister.java index 404497caf..2333ee91a 100644 --- a/spring-data-mongodb-cross-store/src/main/java/org/springframework/data/mongodb/crossstore/MongoChangeSetPersister.java +++ b/spring-data-mongodb-cross-store/src/main/java/org/springframework/data/mongodb/crossstore/MongoChangeSetPersister.java @@ -1,5 +1,5 @@ /* - * Copyright 2011 the original author or authors. + * 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. @@ -34,6 +34,10 @@ import com.mongodb.DBCollection; import com.mongodb.DBObject; import com.mongodb.MongoException; +/** + * @author Thomas Risberg + * @author Oliver Gierke + */ public class MongoChangeSetPersister implements ChangeSetPersister { private static final String ENTITY_CLASS = "_entity_class"; @@ -58,6 +62,10 @@ public class MongoChangeSetPersister implements ChangeSetPersister { this.entityManagerFactory = entityManagerFactory; } + /* + * (non-Javadoc) + * @see org.springframework.data.crossstore.ChangeSetPersister#getPersistentState(java.lang.Class, java.lang.Object, org.springframework.data.crossstore.ChangeSet) + */ public void getPersistentState(Class entityClass, Object id, final ChangeSet changeSet) throws DataAccessException, NotFoundException { @@ -100,6 +108,10 @@ public class MongoChangeSetPersister implements ChangeSetPersister { }); } + /* + * (non-Javadoc) + * @see org.springframework.data.crossstore.ChangeSetPersister#getPersistentId(org.springframework.data.crossstore.ChangeSetBacked, org.springframework.data.crossstore.ChangeSet) + */ public Object getPersistentId(ChangeSetBacked entity, ChangeSet cs) throws DataAccessException { log.debug("getPersistentId called on " + entity); if (entityManagerFactory == null) { @@ -109,6 +121,10 @@ public class MongoChangeSetPersister implements ChangeSetPersister { return o; } + /* + * (non-Javadoc) + * @see org.springframework.data.crossstore.ChangeSetPersister#persistState(org.springframework.data.crossstore.ChangeSetBacked, org.springframework.data.crossstore.ChangeSet) + */ public Object persistState(ChangeSetBacked entity, ChangeSet cs) throws DataAccessException { if (cs == null) { log.debug("Flush: changeset was null, nothing to flush."); @@ -169,8 +185,13 @@ public class MongoChangeSetPersister implements ChangeSetPersister { return 0L; } + /** + * Returns the collection the given entity type shall be persisted to. + * + * @param entityClass must not be {@literal null}. + * @return + */ private String getCollectionNameForEntity(Class entityClass) { - return ClassUtils.getQualifiedName(entityClass); + return mongoTemplate.getCollectionName(entityClass); } - } diff --git a/spring-data-mongodb-cross-store/src/test/java/org/springframework/data/mongodb/crossstore/CrossStoreMongoTests.java b/spring-data-mongodb-cross-store/src/test/java/org/springframework/data/mongodb/crossstore/CrossStoreMongoTests.java index 6dd23ea33..4f2451a15 100644 --- a/spring-data-mongodb-cross-store/src/test/java/org/springframework/data/mongodb/crossstore/CrossStoreMongoTests.java +++ b/spring-data-mongodb-cross-store/src/test/java/org/springframework/data/mongodb/crossstore/CrossStoreMongoTests.java @@ -36,9 +36,14 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.support.TransactionCallback; import org.springframework.transaction.support.TransactionTemplate; -import com.mongodb.DBCollection; import com.mongodb.DBObject; +/** + * Integration tests for MongoDB cross-store persistence (mainly {@link MongoChangeSetPersister}). + * + * @author Thomas Risberg + * @author Oliver Gierke + */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:/META-INF/spring/applicationContext.xml") public class CrossStoreMongoTests { @@ -58,7 +63,7 @@ public class CrossStoreMongoTests { txTemplate = new TransactionTemplate(transactionManager); - clearData(Person.class.getName()); + clearData(Person.class); Address address = new Address(12, "MAin St.", "Boston", "MA", "02101"); @@ -91,11 +96,10 @@ public class CrossStoreMongoTests { }); } - private void clearData(String collectionName) { - DBCollection col = this.mongoTemplate.getCollection(collectionName); - if (col != null) { - this.mongoTemplate.dropCollection(collectionName); - } + private void clearData(Class domainType) { + + String collectionName = mongoTemplate.getCollectionName(domainType); + mongoTemplate.dropCollection(collectionName); } @Test @@ -183,7 +187,7 @@ public class CrossStoreMongoTests { boolean weFound3 = false; - for (DBObject dbo : this.mongoTemplate.getCollection(Person.class.getName()).find()) { + for (DBObject dbo : this.mongoTemplate.getCollection(mongoTemplate.getCollectionName(Person.class)).find()) { Assert.assertTrue(!dbo.get("_entity_id").equals(2L)); if (dbo.get("_entity_id").equals(3L)) { weFound3 = true;