diff --git a/spring-data-mongodb-cross-store/.classpath b/spring-data-mongodb-cross-store/.classpath index 35f990ccf..cad9eafec 100644 --- a/spring-data-mongodb-cross-store/.classpath +++ b/spring-data-mongodb-cross-store/.classpath @@ -1,9 +1,9 @@ - + - + diff --git a/spring-data-mongodb-cross-store/src/main/java/org/springframework/persistence/document/MongoChangeSetPersister.java b/spring-data-mongodb-cross-store/src/main/java/org/springframework/persistence/document/MongoChangeSetPersister.java index 7af1aed73..eb906d323 100644 --- a/spring-data-mongodb-cross-store/src/main/java/org/springframework/persistence/document/MongoChangeSetPersister.java +++ b/spring-data-mongodb-cross-store/src/main/java/org/springframework/persistence/document/MongoChangeSetPersister.java @@ -8,6 +8,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.convert.ConversionService; import org.springframework.dao.DataAccessException; import org.springframework.dao.DataAccessResourceFailureException; +import org.springframework.data.document.mongodb.MongoTemplate; import org.springframework.persistence.support.ChangeSet; import org.springframework.persistence.support.ChangeSetBacked; import org.springframework.persistence.support.ChangeSetPersister; @@ -17,6 +18,7 @@ import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBObject; +import com.mongodb.Mongo; import com.mongodb.MongoException; //import edu.emory.mathcs.backport.java.util.Arrays; @@ -26,11 +28,11 @@ public class MongoChangeSetPersister implements ChangeSetPersister { protected final Log log = LogFactory.getLog(getClass()); @Autowired - private DB mongoDb; + private MongoTemplate mongoTemplate; @Autowired private ConversionService conversionService; - + @Override public void getPersistentState(Class entityClass, Object id, ChangeSet changeSet) throws DataAccessException, NotFoundException { @@ -38,7 +40,7 @@ public class MongoChangeSetPersister implements ChangeSetPersister { DBObject q = new BasicDBObject(); q.put("_id", id); try { - DBObject dbo = mongoDb.getCollection(collection).findOne(q); + DBObject dbo = mongoTemplate.getCollection(collection).findOne(q); if (dbo == null) { throw new NotFoundException(); } @@ -89,17 +91,18 @@ public class MongoChangeSetPersister implements ChangeSetPersister { log.info("Flush: entity make persistent; data store will assign id"); cs.set("_class", entityClass.getName()); String collection = entityClass.getName(); - DBCollection dbc = mongoDb.getCollection(collection); + DBCollection dbc = mongoTemplate.getCollection(collection); DBObject dbo = mapChangeSetToDbObject(cs); if (dbc == null) { - dbc = mongoDb.createCollection(collection, dbo); + dbc = mongoTemplate.createCollection(collection); } dbc.save(dbo); id = dbo.get(ID_KEY); + log.info("Data store assigned id: " + id); } else { log.info("Flush: entity already persistent with id=" + id); String collection = entityClass.getName(); - DBCollection dbc = mongoDb.getCollection(collection); + DBCollection dbc = mongoTemplate.getCollection(collection); DBObject dbo = mapChangeSetToDbObject(cs); if (dbc == null) { throw new DataAccessResourceFailureException("Expected to find a collection named '" + collection +"'. It was not found, so ChangeSet can't be persisted."); diff --git a/spring-data-mongodb-cross-store/src/test/java/org/springframework/data/document/persistence/Account.java b/spring-data-mongodb-cross-store/src/test/java/org/springframework/data/document/persistence/Account.java index 26edd39f3..87b882df2 100644 --- a/spring-data-mongodb-cross-store/src/test/java/org/springframework/data/document/persistence/Account.java +++ b/spring-data-mongodb-cross-store/src/test/java/org/springframework/data/document/persistence/Account.java @@ -1,9 +1,12 @@ package org.springframework.data.document.persistence; import javax.persistence.Entity; +import javax.persistence.Id; @Entity public class Account { + + @Id private Long id; private String name; diff --git a/spring-data-mongodb-cross-store/src/test/java/org/springframework/data/document/persistence/MongoPerson.java b/spring-data-mongodb-cross-store/src/test/java/org/springframework/data/document/persistence/MongoPerson.java index d1ad5e3f8..c1a32ee02 100644 --- a/spring-data-mongodb-cross-store/src/test/java/org/springframework/data/document/persistence/MongoPerson.java +++ b/spring-data-mongodb-cross-store/src/test/java/org/springframework/data/document/persistence/MongoPerson.java @@ -1,11 +1,8 @@ package org.springframework.data.document.persistence; -import javax.persistence.Entity; - import org.springframework.persistence.RelatedEntity; import org.springframework.persistence.document.DocumentEntity; -@Entity @DocumentEntity public class MongoPerson { diff --git a/spring-data-mongodb-cross-store/src/test/resources/META-INF/persistence.xml b/spring-data-mongodb-cross-store/src/test/resources/META-INF/persistence.xml new file mode 100644 index 000000000..6415a90e8 --- /dev/null +++ b/spring-data-mongodb-cross-store/src/test/resources/META-INF/persistence.xml @@ -0,0 +1,13 @@ + + + + org.hibernate.ejb.HibernatePersistence + org.springframework.data.document.persistence.Account + + + + + + + + diff --git a/spring-data-mongodb-cross-store/src/test/resources/META-INF/spring/applicationContext.xml b/spring-data-mongodb-cross-store/src/test/resources/META-INF/spring/applicationContext.xml index 4ab9c2012..da626d90e 100644 --- a/spring-data-mongodb-cross-store/src/test/resources/META-INF/spring/applicationContext.xml +++ b/spring-data-mongodb-cross-store/src/test/resources/META-INF/spring/applicationContext.xml @@ -2,7 +2,9 @@ @@ -11,9 +13,9 @@ - - - + + + @@ -23,13 +25,17 @@ factory-method="aspectOf"> + + + + + + + + - - - - - - + + @@ -38,7 +44,15 @@ + + + + + + + + diff --git a/spring-data-mongodb-cross-store/src/test/resources/log4j.properties b/spring-data-mongodb-cross-store/src/test/resources/log4j.properties new file mode 100644 index 000000000..292bb1d4d --- /dev/null +++ b/spring-data-mongodb-cross-store/src/test/resources/log4j.properties @@ -0,0 +1,13 @@ +log4j.rootCategory=INFO, stdout + +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout +log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n + +log4j.category.org.springframework=INFO +log4j.category.org.springframework.data=DEBUG +log4j.category.org.springframework.persistence=DEBUG + +log4j.category.org.hibernate.SQL=DEBUG +# for debugging datasource initialization +# log4j.category.test.jdbc=DEBUG