DATADOC-48 added advice for moving changeset from detached entity to newly merged persistent entity

This commit is contained in:
Thomas Risberg
2011-03-31 13:43:13 -04:00
parent 8723d85570
commit f9f26e7668
6 changed files with 114 additions and 34 deletions

View File

@@ -8,8 +8,8 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.document.mongodb.MongoTemplate;
import org.springframework.persistence.document.test.Person;
import org.springframework.persistence.document.test.Resume;
import org.springframework.data.document.persistence.test.Person;
import org.springframework.data.document.persistence.test.Resume;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -99,18 +99,53 @@ public class CrossStoreMongoTests {
@Test
public void testMergeJpaEntityWithMongoDocument() {
TransactionTemplate txTemplate = new TransactionTemplate(transactionManager);
final Person found = entityManager.find(Person.class, 1L);
found.setAge(77);
found.getResume().addJob("TargetRx, Developer, 2000-2005");
txTemplate.execute(new TransactionCallback<Object>() {
public Object doInTransaction(TransactionStatus status) {
entityManager.merge(found);
final Person detached = entityManager.find(Person.class, 1L);
detached.getResume().addJob("TargetRx, Developer, 2000-2005");
Person merged = txTemplate.execute(new TransactionCallback<Person>() {
public Person doInTransaction(TransactionStatus status) {
return entityManager.merge(detached);
}
});
Assert.assertTrue(detached.getResume().getJobs().contains("TargetRx, Developer, 2000-2005"));
Assert.assertTrue(merged.getResume().getJobs().contains("TargetRx, Developer, 2000-2005"));
final Person updated = entityManager.find(Person.class, 1L);
Assert.assertTrue(updated.getResume().getJobs().contains("TargetRx, Developer, 2000-2005"));
}
@Test
public void testRemoveJpaEntityWithMongoDocument() {
TransactionTemplate txTemplate = new TransactionTemplate(transactionManager);
txTemplate.execute(new TransactionCallback<Person>() {
public Person doInTransaction(TransactionStatus status) {
Person p2 = new Person("Thomas", 20);
Resume r2 = new Resume();
r2.addEducation("Skanstulls High School, 1975");
r2.addJob("DiMark, DBA, 1990-2000");
p2.setResume(r2);
p2.setId(2L);
entityManager.persist(p2);
Person p3 = new Person("Thomas", 20);
Resume r3 = new Resume();
r3.addEducation("Univ. of Stockholm, 1980");
r3.addJob("VMware, Developer, 2007-");
p3.setResume(r3);
p3.setId(3L);
entityManager.persist(p3);
return null;
}
});
final Person updated = entityManager.find(Person.class, 1L);
// assert that the new values are in respective DBs
// TODO: during merge we lose the changeset since JPA creates a new persistent instance -
// we need to move the existing changeset over somehow
txTemplate.execute(new TransactionCallback<Person>() {
public Person doInTransaction(TransactionStatus status) {
final Person found2 = entityManager.find(Person.class, 2L);
final Person found3 = entityManager.find(Person.class, 3L);
entityManager.remove(found2);
return null;
}
});
final Person found2 = entityManager.find(Person.class, 2L);
final Person found3 = entityManager.find(Person.class, 3L);
// TODO: assert that any documents for Person 2 are gone
// System.out.println(found2);
// System.out.println(found3);
}
}

View File

@@ -1,4 +1,4 @@
package org.springframework.persistence.document.test;
package org.springframework.data.document.persistence.test;
import javax.persistence.Entity;
import javax.persistence.Id;

View File

@@ -1,8 +1,14 @@
package org.springframework.persistence.document.test;
package org.springframework.data.document.persistence.test;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.data.persistence.document.mongo.MongoDocumentBacking;
//@DocumentEntity
public class Resume {
private static final Log LOGGER = LogFactory.getLog(Resume.class);
private String education = "";
private String jobs = "";
@@ -12,6 +18,7 @@ public class Resume {
}
public void addEducation(String education) {
LOGGER.debug("Adding education " + education);
this.education = this.education + (this.education.length() > 0 ? "; " : "") + education;
}
@@ -20,6 +27,7 @@ public class Resume {
}
public void addJob(String job) {
LOGGER.debug("Adding job " + job);
this.jobs = this.jobs + (this.jobs.length() > 0 ? "; " : "") + job;
}

View File

@@ -4,7 +4,7 @@
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>org.springframework.persistence.document.test.Person</class>
<class>org.springframework.data.document.persistence.test.Person</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<!--value='create' to build a new database on each run; value='update' to modify an existing database; value='create-drop' means the same as 'create' but also drops tables when Hibernate closes; value='validate' makes no changes to the database-->