@@ -44,11 +44,8 @@ public aspect MongoDocumentBacking {
44
44
// ITD to introduce N state to Annotated objects
45
45
declare parents : (@Entity * ) implements DocumentBacked;
46
46
47
- // declare @type: DocumentBacked+: @Configurable;
48
-
47
+ // The annotated fields that will be persisted in MongoDB rather than with JPA
49
48
declare @field : @Document * (@Entity + * ). * : @Transient ;
50
- declare @field : ChangeSet (DocumentBacked + ). * : @Transient ;
51
- declare @field : ChangeSetPersister (DocumentBacked + ). * : @Transient ;
52
49
53
50
// -------------------------------------------------------------------------
54
51
// Advise user-defined constructors of ChangeSetBacked objects to create a new
@@ -86,27 +83,44 @@ public aspect MongoDocumentBacking {
86
83
LOGGER
87
84
.debug(" User-defined constructor called on DocumentBacked object of class "
88
85
+ entity. getClass());
86
+ // Populate all ITD fields
87
+ entity. setChangeSet(new HashMapChangeSet ());
89
88
entity. itdChangeSetPersister = changeSetPersister;
90
- // Populate all properties
91
- ChangeSet changeSet = new HashMapChangeSet ();
92
- // changeSetManager.populateChangeSet(changeSet, entity);
93
- entity. setChangeSet(changeSet);
94
- if (! TransactionSynchronizationManager . isSynchronizationActive()) {
95
- throw new InvalidDataAccessResourceUsageException (
96
- " No transaction synchronization is active" );
89
+ entity. itdTransactionSynchronization =
90
+ new DocumentBackedTransactionSynchronization (changeSetPersister, entity);
91
+ registerTransactionSynchronization(entity);
92
+ }
93
+
94
+ private static void registerTransactionSynchronization (DocumentBacked entity ) {
95
+ if (TransactionSynchronizationManager . isSynchronizationActive()) {
96
+ if (! TransactionSynchronizationManager . getSynchronizations(). contains(entity. itdTransactionSynchronization)) {
97
+ if (LOGGER . isDebugEnabled()) {
98
+ LOGGER . debug(" Adding transaction synchronization for " + entity. getClass());
99
+ }
100
+ TransactionSynchronizationManager . registerSynchronization(entity. itdTransactionSynchronization);
101
+ }
102
+ else {
103
+ if (LOGGER . isDebugEnabled()) {
104
+ LOGGER . debug(" Transaction synchronization already active for " + entity. getClass());
105
+ }
106
+ }
107
+ }
108
+ else {
109
+ if (LOGGER . isDebugEnabled()) {
110
+ LOGGER . debug(" Transaction syncronization is not active for " + entity. getClass());
111
+ }
97
112
}
98
- TransactionSynchronizationManager
99
- .registerSynchronization(new DocumentBackedTransactionSynchronization (
100
- changeSetPersister, entity));
101
113
}
102
114
103
115
// -------------------------------------------------------------------------
104
116
// ChangeSet-related mixins
105
117
// -------------------------------------------------------------------------
106
118
// Introduced field
107
- private ChangeSet DocumentBacked . changeSet;
119
+ @Transient private ChangeSet DocumentBacked . changeSet;
108
120
109
- private ChangeSetPersister<?> DocumentBacked . itdChangeSetPersister;
121
+ @Transient private ChangeSetPersister<?> DocumentBacked . itdChangeSetPersister;
122
+
123
+ @Transient private DocumentBackedTransactionSynchronization DocumentBacked . itdTransactionSynchronization;
110
124
111
125
public void DocumentBacked.setChangeSet (ChangeSet cs ) {
112
126
this . changeSet = cs;
@@ -126,6 +140,32 @@ public aspect MongoDocumentBacking {
126
140
return itdChangeSetPersister. getPersistentId(this , this . changeSet);
127
141
}
128
142
143
+ // lifecycle methods
144
+ @javax . persistence.PrePersist public void DocumentBacked.prePersist () {
145
+ if (LOGGER . isDebugEnabled()) {
146
+ LOGGER . debug(" JPA lifecycle called PrePersist: " + this . getClass(). getName() + " :: " + this . get_persistent_id());
147
+ }
148
+ registerTransactionSynchronization(this );
149
+ }
150
+ @javax . persistence.PreUpdate public void DocumentBacked.preUpdate () {
151
+ if (LOGGER . isDebugEnabled()) {
152
+ LOGGER . debug(" JPA lifecycle called PreUpdate: " + this . getClass(). getName() + " :: " + this . get_persistent_id());
153
+ }
154
+ registerTransactionSynchronization(this );
155
+ }
156
+ @javax . persistence.PreRemove public void DocumentBacked.preRemove () {
157
+ if (LOGGER . isDebugEnabled()) {
158
+ LOGGER . debug(" JPA lifecycle called PreRemove: " + this . getClass(). getName() + " :: " + this . get_persistent_id());
159
+ }
160
+ registerTransactionSynchronization(this );
161
+ }
162
+ @javax . persistence.PostLoad public void DocumentBacked.postLoad () {
163
+ if (LOGGER . isDebugEnabled()) {
164
+ LOGGER . debug(" JPA lifecycle called PostLoad: " + this . getClass(). getName() + " :: " + this . get_persistent_id());
165
+ }
166
+ registerTransactionSynchronization(this );
167
+ }
168
+
129
169
/**
130
170
* delegates field reads to the state accessors instance
131
171
*/
@@ -160,18 +200,6 @@ public aspect MongoDocumentBacking {
160
200
return proceed (entity, newVal);
161
201
}
162
202
163
- // /**
164
- // * delegates field writes to the state accessors instance
165
- // */
166
- // Object around(DocumentBacked entity, Object newVal) : entityIdSet(entity, newVal) {
167
- // Field f = field(thisJoinPoint);
168
- // String propName = f.getName();
169
- // LOGGER.trace("SET @Id -> ChangeSet @Id property [" + propName
170
- // + "] with value=[" + newVal + "]");
171
- // entity.getChangeSet().set("_id", newVal);
172
- // return proceed(entity, newVal);
173
- // }
174
-
175
203
Field field (JoinPoint joinPoint ) {
176
204
FieldSignature fieldSignature = (FieldSignature ) joinPoint. getSignature();
177
205
return fieldSignature. getField();
0 commit comments