Browse Source

Merge remote branch 'origin/master'

pull/1/head
Mark Pollack 15 years ago
parent
commit
a11218cd9c
  1. 44
      spring-data-mongodb-cross-store/src/main/java/org/springframework/data/persistence/document/mongo/MongoDocumentBacking.aj
  2. 64
      src/docbkx/reference/cross-store.xml

44
spring-data-mongodb-cross-store/src/main/java/org/springframework/data/persistence/document/mongo/MongoDocumentBacking.aj

@ -82,10 +82,10 @@ public aspect MongoDocumentBacking { @@ -82,10 +82,10 @@ public aspect MongoDocumentBacking {
args(entity);
// intercept EntityManager.remove calls
public pointcut entityManagerRemove(EntityManager em, Object entity) :
call(* EntityManager.remove(Object)) &&
target(em) &&
args(entity);
// public pointcut entityManagerRemove(EntityManager em, Object entity) :
// call(* EntityManager.remove(Object)) &&
// target(em) &&
// args(entity);
// move changeSet from detached entity to the newly merged persistent object
Object around(EntityManager em, Object entity) : entityManagerMerge(em, entity) {
@ -97,26 +97,31 @@ public aspect MongoDocumentBacking { @@ -97,26 +97,31 @@ public aspect MongoDocumentBacking {
}
// clear changeSet from removed entity
Object around(EntityManager em, Object entity) : entityManagerRemove(em, entity) {
if (entity instanceof DocumentBacked) {
ChangeSet nulledCs = new HashMapChangeSet();
DocumentBacked documentEntity = (DocumentBacked) entity;
@SuppressWarnings("unchecked")
ChangeSetPersister<Object> changeSetPersister = (ChangeSetPersister<Object>)documentEntity.itdChangeSetPersister;
try {
// Object around(EntityManager em, Object entity) : entityManagerRemove(em, entity) {
// if (entity instanceof DocumentBacked) {
// removeChangeSetValues((DocumentBacked)entity);
// }
// return proceed(em, entity);
// }
private static void removeChangeSetValues(DocumentBacked entity) {
LOGGER.debug("Removing all change-set values for " + entity);
ChangeSet nulledCs = new HashMapChangeSet();
DocumentBacked documentEntity = (DocumentBacked) entity;
@SuppressWarnings("unchecked")
ChangeSetPersister<Object> changeSetPersister = (ChangeSetPersister<Object>)documentEntity.itdChangeSetPersister;
try {
changeSetPersister.getPersistentState(
documentEntity.getClass(),
documentEntity.get_persistent_id(),
documentEntity.getChangeSet());
}
catch (DataAccessException e) {}
catch (NotFoundException e) {}
for (String key : ((DocumentBacked)entity).getChangeSet().getValues().keySet()) {
}
catch (DataAccessException e) {}
catch (NotFoundException e) {}
for (String key :entity.getChangeSet().getValues().keySet()) {
nulledCs.set(key, null);
}
((DocumentBacked)entity).setChangeSet(nulledCs);
}
return proceed(em, entity);
}
entity.setChangeSet(nulledCs);
}
before(DocumentBacked entity) : arbitraryUserConstructorOfChangeSetBackedObject(entity) {
@ -204,6 +209,7 @@ public aspect MongoDocumentBacking { @@ -204,6 +209,7 @@ public aspect MongoDocumentBacking {
LOGGER.debug("JPA lifecycle event PostRemove: " + this.getClass().getName() + " :: " + this);
}
registerTransactionSynchronization(this);
removeChangeSetValues(this);
}
@javax.persistence.PostLoad public void DocumentBacked.itdPostLoad() {
if (LOGGER.isDebugEnabled()) {

64
src/docbkx/reference/cross-store.xml

@ -191,11 +191,12 @@ @@ -191,11 +191,12 @@
persisted in MongoDB should be annotated using the
<classname>@RelatedDocument</classname> annotation. Well, that is really
all you need to do. The cross-store aspects take care of the rest. This
include marking the field with @Transient so it won't be persisted using
JPA, keeping track of any changes and flushing them on succesfull
transaction completion, loading teh document for MongoDB when the values
is used in your application. Here is an example of a simple Entity that
has a field annotated with @RelatedEntity.</para>
includes marking the field with @Transient so it won't be persisted using
JPA, keeping track of any changes made to the field value and writing them
to the database on succesfull transaction completion, loading teh document
from MongoDB the first time the value is used in your application. Here is
an example of a simple Entity that has a field annotated with
@RelatedEntity.</para>
<example>
<title>Example of Entity with @RelatedDocument</title>
@ -226,6 +227,14 @@ public class Customer { @@ -226,6 +227,14 @@ public class Customer {
private Map&lt;String, String&gt; questionsAndAnswers;
public SurveyInfo() {
this.questionsAndAnswers = new HashMap&lt;String, String&gt;();
}
public SurveyInfo(Map&lt;String, String&gt; questionsAndAnswers) {
this.questionsAndAnswers = questionsAndAnswers;
}
public Map&lt;String, String&gt; getQuestionsAndAnswers() {
return questionsAndAnswers;
}
@ -233,14 +242,51 @@ public class Customer { @@ -233,14 +242,51 @@ public class Customer {
public void setQuestionsAndAnswers(Map&lt;String, String&gt; questionsAndAnswers) {
this.questionsAndAnswers = questionsAndAnswers;
}
} </programlisting>
public SurveyInfo addQuestionAndAnswer(String question, String answer) {
this.questionsAndAnswers.put(question, answer);
return this;
}
} </programlisting>
</example>
<para>Once t...</para>
<para>Once the SurveyInfo has been set on the Customer object above the
MongoTemplate that was configured above is used to save the SurveyInfo
along with some metadata about the JPA Entity is stored in a MongoDB
collection named after the fully qualified name of the JPA Entity class.
The following code:</para>
<para></para>
<example>
<title>Example of code using the JPA Entity configured for cross-store
persistence</title>
<programlisting language="java"> Customer customer = new Customer();
customer.setFirstName("Sven");
customer.setLastName("Olafsen");
SurveyInfo surveyInfo = new SurveyInfo()
.addQuestionAndAnswer("age", "22")
.addQuestionAndAnswer("married", "Yes")
.addQuestionAndAnswer("citizenship", "Norwegian");
customer.setSurveyInfo(surveyInfo);
customerRepository.save(customer);
</programlisting>
</example>
<para></para>
<para>Executing the code above results in the following JSON document
stored in MongoDB.</para>
<example>
<title>Example of JSON document stored in MongoDB</title>
<programlisting language="javascript">{ "_id" : ObjectId( "4d9e8b6e3c55287f87d4b79e" ),
"_entity_id" : 1,
"_entity_class" : "org.springframework.data.mongodb.examples.custsvc.domain.Customer",
"_entity_field_name" : "surveyInfo",
"questionsAndAnswers" : { "married" : "Yes",
"age" : "22",
"citizenship" : "Norwegian" },
"_entity_field_class" : "org.springframework.data.mongodb.examples.custsvc.domain.SurveyInfo" }</programlisting>
</example>
<para></para>
</section>

Loading…
Cancel
Save