Skip to content

Commit 04e0f5c

Browse files
committed
DATAMONGO-642 - MongoChangeSetPersister now considers mapped collection.
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 = "…")).
1 parent 133975f commit 04e0f5c

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

spring-data-mongodb-cross-store/src/main/java/org/springframework/data/mongodb/crossstore/MongoChangeSetPersister.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011 the original author or authors.
2+
* Copyright 2011-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,6 +34,10 @@
3434
import com.mongodb.DBObject;
3535
import com.mongodb.MongoException;
3636

37+
/**
38+
* @author Thomas Risberg
39+
* @author Oliver Gierke
40+
*/
3741
public class MongoChangeSetPersister implements ChangeSetPersister<Object> {
3842

3943
private static final String ENTITY_CLASS = "_entity_class";
@@ -58,6 +62,10 @@ public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) {
5862
this.entityManagerFactory = entityManagerFactory;
5963
}
6064

65+
/*
66+
* (non-Javadoc)
67+
* @see org.springframework.data.crossstore.ChangeSetPersister#getPersistentState(java.lang.Class, java.lang.Object, org.springframework.data.crossstore.ChangeSet)
68+
*/
6169
public void getPersistentState(Class<? extends ChangeSetBacked> entityClass, Object id, final ChangeSet changeSet)
6270
throws DataAccessException, NotFoundException {
6371

@@ -100,6 +108,10 @@ public Object doInCollection(DBCollection collection) throws MongoException, Dat
100108
});
101109
}
102110

111+
/*
112+
* (non-Javadoc)
113+
* @see org.springframework.data.crossstore.ChangeSetPersister#getPersistentId(org.springframework.data.crossstore.ChangeSetBacked, org.springframework.data.crossstore.ChangeSet)
114+
*/
103115
public Object getPersistentId(ChangeSetBacked entity, ChangeSet cs) throws DataAccessException {
104116
log.debug("getPersistentId called on " + entity);
105117
if (entityManagerFactory == null) {
@@ -109,6 +121,10 @@ public Object getPersistentId(ChangeSetBacked entity, ChangeSet cs) throws DataA
109121
return o;
110122
}
111123

124+
/*
125+
* (non-Javadoc)
126+
* @see org.springframework.data.crossstore.ChangeSetPersister#persistState(org.springframework.data.crossstore.ChangeSetBacked, org.springframework.data.crossstore.ChangeSet)
127+
*/
112128
public Object persistState(ChangeSetBacked entity, ChangeSet cs) throws DataAccessException {
113129
if (cs == null) {
114130
log.debug("Flush: changeset was null, nothing to flush.");
@@ -169,8 +185,13 @@ public Object doInCollection(DBCollection collection) throws MongoException, Dat
169185
return 0L;
170186
}
171187

188+
/**
189+
* Returns the collection the given entity type shall be persisted to.
190+
*
191+
* @param entityClass must not be {@literal null}.
192+
* @return
193+
*/
172194
private String getCollectionNameForEntity(Class<? extends ChangeSetBacked> entityClass) {
173-
return ClassUtils.getQualifiedName(entityClass);
195+
return mongoTemplate.getCollectionName(entityClass);
174196
}
175-
176197
}

spring-data-mongodb-cross-store/src/test/java/org/springframework/data/mongodb/crossstore/CrossStoreMongoTests.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,14 @@
3636
import org.springframework.transaction.support.TransactionCallback;
3737
import org.springframework.transaction.support.TransactionTemplate;
3838

39-
import com.mongodb.DBCollection;
4039
import com.mongodb.DBObject;
4140

41+
/**
42+
* Integration tests for MongoDB cross-store persistence (mainly {@link MongoChangeSetPersister}).
43+
*
44+
* @author Thomas Risberg
45+
* @author Oliver Gierke
46+
*/
4247
@RunWith(SpringJUnit4ClassRunner.class)
4348
@ContextConfiguration("classpath:/META-INF/spring/applicationContext.xml")
4449
public class CrossStoreMongoTests {
@@ -58,7 +63,7 @@ public void setUp() {
5863

5964
txTemplate = new TransactionTemplate(transactionManager);
6065

61-
clearData(Person.class.getName());
66+
clearData(Person.class);
6267

6368
Address address = new Address(12, "MAin St.", "Boston", "MA", "02101");
6469

@@ -91,11 +96,10 @@ public Void doInTransaction(TransactionStatus status) {
9196
});
9297
}
9398

94-
private void clearData(String collectionName) {
95-
DBCollection col = this.mongoTemplate.getCollection(collectionName);
96-
if (col != null) {
97-
this.mongoTemplate.dropCollection(collectionName);
98-
}
99+
private void clearData(Class<?> domainType) {
100+
101+
String collectionName = mongoTemplate.getCollectionName(domainType);
102+
mongoTemplate.dropCollection(collectionName);
99103
}
100104

101105
@Test
@@ -183,7 +187,7 @@ public Person doInTransaction(TransactionStatus status) {
183187

184188
boolean weFound3 = false;
185189

186-
for (DBObject dbo : this.mongoTemplate.getCollection(Person.class.getName()).find()) {
190+
for (DBObject dbo : this.mongoTemplate.getCollection(mongoTemplate.getCollectionName(Person.class)).find()) {
187191
Assert.assertTrue(!dbo.get("_entity_id").equals(2L));
188192
if (dbo.get("_entity_id").equals(3L)) {
189193
weFound3 = true;

0 commit comments

Comments
 (0)