diff --git a/spring-data-mongodb-cross-store/src/main/java/org/springframework/data/persistence/document/DocumentBackedTransactionSynchronization.java b/spring-data-mongodb-cross-store/src/main/java/org/springframework/data/persistence/document/DocumentBackedTransactionSynchronization.java index ee5fb3aca..4b0f887b5 100644 --- a/spring-data-mongodb-cross-store/src/main/java/org/springframework/data/persistence/document/DocumentBackedTransactionSynchronization.java +++ b/spring-data-mongodb-cross-store/src/main/java/org/springframework/data/persistence/document/DocumentBackedTransactionSynchronization.java @@ -26,7 +26,7 @@ public class DocumentBackedTransactionSynchronization implements TransactionSync @Override public void afterCommit() { log.debug("After Commit called for " + entity); - changeSetPersister.persistState(entity.getClass(), entity.getChangeSet()); + changeSetPersister.persistState(entity, entity.getChangeSet()); changeSetTxStatus = 0; } diff --git a/spring-data-mongodb-cross-store/src/main/java/org/springframework/data/persistence/document/mongo/MongoChangeSetPersister.java b/spring-data-mongodb-cross-store/src/main/java/org/springframework/data/persistence/document/mongo/MongoChangeSetPersister.java index f793aea7a..8207052c6 100644 --- a/spring-data-mongodb-cross-store/src/main/java/org/springframework/data/persistence/document/mongo/MongoChangeSetPersister.java +++ b/spring-data-mongodb-cross-store/src/main/java/org/springframework/data/persistence/document/mongo/MongoChangeSetPersister.java @@ -1,5 +1,7 @@ package org.springframework.data.persistence.document.mongo; +import javax.persistence.EntityManagerFactory; + import com.mongodb.BasicDBObject; import com.mongodb.DBCollection; import com.mongodb.DBObject; @@ -7,6 +9,7 @@ import com.mongodb.MongoException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.dao.DataAccessException; +import org.springframework.dao.DataAccessResourceFailureException; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.data.document.mongodb.CollectionCallback; import org.springframework.data.document.mongodb.MongoTemplate; @@ -29,14 +32,27 @@ public class MongoChangeSetPersister implements ChangeSetPersister { private MongoTemplate mongoTemplate; + private EntityManagerFactory entityManagerFactory; + public void setMongoTemplate(MongoTemplate mongoTemplate) { this.mongoTemplate = mongoTemplate; } + public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) { + this.entityManagerFactory = entityManagerFactory; + } + + @Override public void getPersistentState(Class entityClass, - Object id, final ChangeSet changeSet) throws DataAccessException, - NotFoundException { + Object id, final ChangeSet changeSet) + throws DataAccessException, NotFoundException { + + if (id == null) { + log.debug("Unable to load MongoDB data for null id"); + return; + } + String collName = getCollectionNameForEntity(entityClass); final DBObject dbk = new BasicDBObject(); @@ -75,26 +91,20 @@ public class MongoChangeSetPersister implements ChangeSetPersister { } @Override - public Object getPersistentId(Class entityClass, - ChangeSet cs) throws DataAccessException { - log.debug("getPersistentId called on " + entityClass); - if (cs == null) { - return null; + public Object getPersistentId(ChangeSetBacked entity, ChangeSet cs) throws DataAccessException { + log.debug("getPersistentId called on " + entity); + if (entityManagerFactory == null) { + throw new DataAccessResourceFailureException("EntityManagerFactory cannot be null"); } - if (cs.getValues().get(ChangeSetPersister.ID_KEY) == null) { - // Not yet persistent - return null; - } - Object o = cs.getValues().get(ChangeSetPersister.ID_KEY); + Object o = entityManagerFactory.getPersistenceUnitUtil().getIdentifier(entity); return o; } @Override - public Object persistState(Class entityClass, - ChangeSet cs) throws DataAccessException { + public Object persistState(ChangeSetBacked entity, ChangeSet cs) throws DataAccessException { log.debug("Flush: changeset: " + cs.getValues().keySet()); - String collName = getCollectionNameForEntity(entityClass); + String collName = getCollectionNameForEntity(entity.getClass()); DBCollection dbc = mongoTemplate.getCollection(collName); if (dbc == null) { dbc = mongoTemplate.createCollection(collName); @@ -103,8 +113,8 @@ public class MongoChangeSetPersister implements ChangeSetPersister { if (key != null && !key.startsWith("_") && !key.equals(ChangeSetPersister.ID_KEY)) { Object value = cs.getValues().get(key); final DBObject dbQuery = new BasicDBObject(); - dbQuery.put(ENTITY_ID, cs.getValues().get(ChangeSetPersister.ID_KEY)); - dbQuery.put(ENTITY_CLASS, entityClass.getName()); + dbQuery.put(ENTITY_ID, getPersistentId(entity, cs)); + dbQuery.put(ENTITY_CLASS, entity.getClass().getName()); dbQuery.put(ENTITY_FIELD_NAME, key); dbQuery.put(ENTITY_FIELD_CLASS, value.getClass().getName()); DBObject dbId = mongoTemplate.execute(collName, @@ -134,8 +144,7 @@ public class MongoChangeSetPersister implements ChangeSetPersister { return 0L; } - private String getCollectionNameForEntity( - Class entityClass) { + private String getCollectionNameForEntity(Class entityClass) { return ClassUtils.getQualifiedName(entityClass); } diff --git a/spring-data-mongodb-cross-store/src/main/java/org/springframework/data/persistence/document/mongo/MongoDocumentBacking.aj b/spring-data-mongodb-cross-store/src/main/java/org/springframework/data/persistence/document/mongo/MongoDocumentBacking.aj index 4bd021b57..e98e8395c 100644 --- a/spring-data-mongodb-cross-store/src/main/java/org/springframework/data/persistence/document/mongo/MongoDocumentBacking.aj +++ b/spring-data-mongodb-cross-store/src/main/java/org/springframework/data/persistence/document/mongo/MongoDocumentBacking.aj @@ -76,11 +76,11 @@ public aspect MongoDocumentBacking { args(newVal) && !set(* DocumentBacked.*); - protected pointcut entityIdSet(DocumentBacked entity, Object newVal) : - set(@Id * DocumentBacked+.*) && - this(entity) && - args(newVal) && - !set(* DocumentBacked.*); +// protected pointcut entityIdSet(DocumentBacked entity, Object newVal) : +// set(@Id * DocumentBacked+.*) && +// this(entity) && +// args(newVal) && +// !set(* DocumentBacked.*); before(DocumentBacked entity) : arbitraryUserConstructorOfChangeSetBackedObject(entity) { LOGGER @@ -118,12 +118,12 @@ public aspect MongoDocumentBacking { // Flush the entity state to the persistent store public void DocumentBacked.flush() { - itdChangeSetPersister.persistState(this.getClass(), this.changeSet); + Object id = itdChangeSetPersister.getPersistentId(this, this.changeSet); + itdChangeSetPersister.persistState(this, this.changeSet); } public Object DocumentBacked.get_persistent_id() { - return itdChangeSetPersister.getPersistentId(this.getClass(), - this.changeSet); + return itdChangeSetPersister.getPersistentId(this, this.changeSet); } /** @@ -160,17 +160,17 @@ public aspect MongoDocumentBacking { return proceed(entity, newVal); } - /** - * delegates field writes to the state accessors instance - */ - Object around(DocumentBacked entity, Object newVal) : entityIdSet(entity, newVal) { - Field f = field(thisJoinPoint); - String propName = f.getName(); - LOGGER.trace("SET @Id -> ChangeSet @Id property [" + propName - + "] with value=[" + newVal + "]"); - entity.getChangeSet().set("_id", newVal); - return proceed(entity, newVal); - } +// /** +// * delegates field writes to the state accessors instance +// */ +// Object around(DocumentBacked entity, Object newVal) : entityIdSet(entity, newVal) { +// Field f = field(thisJoinPoint); +// String propName = f.getName(); +// LOGGER.trace("SET @Id -> ChangeSet @Id property [" + propName +// + "] with value=[" + newVal + "]"); +// entity.getChangeSet().set("_id", newVal); +// return proceed(entity, newVal); +// } Field field(JoinPoint joinPoint) { FieldSignature fieldSignature = (FieldSignature) joinPoint.getSignature(); diff --git a/spring-data-mongodb-cross-store/src/test/java/org/springframework/data/document/persistence/CrossStoreMongoTests.java b/spring-data-mongodb-cross-store/src/test/java/org/springframework/data/document/persistence/CrossStoreMongoTests.java index f9ccfd018..b7fc0036e 100644 --- a/spring-data-mongodb-cross-store/src/test/java/org/springframework/data/document/persistence/CrossStoreMongoTests.java +++ b/spring-data-mongodb-cross-store/src/test/java/org/springframework/data/document/persistence/CrossStoreMongoTests.java @@ -64,11 +64,6 @@ public class CrossStoreMongoTests { public void testReadJpaToMongoEntityRelationship() { Person found = entityManager.find(Person.class, 1L); Assert.assertNotNull(found); - - // TODO: This part isn't quite working yet - need to intercept the id - // population from JPA EM - found.setId(found.getId()); - Assert.assertEquals(Long.valueOf(1), found.getId()); Assert.assertNotNull(found); Assert.assertEquals(Long.valueOf(1), found.getId()); @@ -84,11 +79,6 @@ public class CrossStoreMongoTests { public void testUpdatedJpaToMongoEntityRelationship() { Person found = entityManager.find(Person.class, 1L); Assert.assertNotNull(found); - - // TODO: This part isn't quite working yet - need to intercept the id - // population from JPA EM - found.setId(found.getId()); - Assert.assertEquals(Long.valueOf(1), found.getId()); Assert.assertNotNull(found); Assert.assertEquals(Long.valueOf(1), found.getId()); 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 ac3855ea8..25cdb9d45 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 @@ -36,6 +36,7 @@ +