/
CollectionMapping.java
301 lines (271 loc) · 12 KB
/
CollectionMapping.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/**********************************************************************
Copyright (c) 2002 Kelly Grizzle (TJDO) and others. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Contributors:
2003 Andy Jefferson - coding standards
2005 Andy Jefferson - basic serialisation support
2005 Andy Jefferson - updated serialisation using SCOUtils methods
2005 Andy Jefferson - changed to allow the store to be set or list
...
**********************************************************************/
package org.datanucleus.store.rdbms.mapping.java;
import java.util.Collection;
import org.datanucleus.ExecutionContext;
import org.datanucleus.PropertyNames;
import org.datanucleus.exceptions.NucleusException;
import org.datanucleus.metadata.AbstractMemberMetaData;
import org.datanucleus.metadata.RelationType;
import org.datanucleus.state.ObjectProvider;
import org.datanucleus.store.exceptions.ReachableObjectNotCascadedException;
import org.datanucleus.store.rdbms.mapping.MappingCallbacks;
import org.datanucleus.store.scostore.CollectionStore;
import org.datanucleus.store.types.SCO;
import org.datanucleus.store.types.SCOContainer;
import org.datanucleus.store.types.SCOUtils;
import org.datanucleus.store.types.wrappers.backed.BackedSCO;
import org.datanucleus.util.NucleusLogger;
/**
* Mapping for Collection/Set/List types.
*/
public class CollectionMapping extends AbstractContainerMapping implements MappingCallbacks
{
/**
* Accessor for the Java type represented here.
* @return The java type
*/
public Class getJavaType()
{
return Collection.class;
}
// --------------- Implementation of MappingCallbacks -------------------
public void insertPostProcessing(ObjectProvider ownerOP)
{
}
/**
* Method to be called after the insert of the owner class element.
* @param ownerOP ObjectProvider of the owner
*/
public void postInsert(ObjectProvider ownerOP)
{
ExecutionContext ec = ownerOP.getExecutionContext();
Collection value = (Collection) ownerOP.provideField(getAbsoluteFieldNumber());
if (containerIsStoredInSingleColumn())
{
// Make sure the elements are ok for proceeding
SCOUtils.validateObjectsForWriting(ec, value);
return;
}
if (value == null)
{
// replace null collections with an empty SCO wrapper
replaceFieldWithWrapper(ownerOP, null, false, false);
return;
}
Object[] collElements = value.toArray();
if (!mmd.isCascadePersist())
{
// Field doesnt support cascade-persist so no reachability
if (NucleusLogger.PERSISTENCE.isDebugEnabled())
{
NucleusLogger.PERSISTENCE.debug(LOCALISER.msg("007006", mmd.getFullFieldName()));
}
// Check for any persistable elements that arent persistent
for (int i=0;i<collElements.length;i++)
{
if (!ec.getApiAdapter().isDetached(collElements[i]) &&
!ec.getApiAdapter().isPersistent(collElements[i]))
{
// Element is not persistent so throw exception
throw new ReachableObjectNotCascadedException(mmd.getFullFieldName(), collElements[i]);
}
}
replaceFieldWithWrapper(ownerOP, value, false, false);
}
else
{
// Reachability
if (NucleusLogger.PERSISTENCE.isDebugEnabled())
{
NucleusLogger.PERSISTENCE.debug(LOCALISER.msg("007007", mmd.getFullFieldName()));
}
// Check if some elements need attaching
// TODO Investigate if we can just use the attachCopy route below and skip off this check
boolean needsAttaching = false;
for (int i=0;i<collElements.length;i++)
{
if (ownerOP.getExecutionContext().getApiAdapter().isDetached(collElements[i]))
{
needsAttaching = true;
break;
}
}
if (needsAttaching)
{
// Create a wrapper and attach the elements (and add the others)
SCO collWrapper = replaceFieldWithWrapper(ownerOP, null, false, false);
collWrapper.attachCopy(value);
}
else
{
if (value.size() > 0)
{
// Add the elements direct to the datastore
((CollectionStore) storeMgr.getBackingStoreForField(ownerOP.getExecutionContext().getClassLoaderResolver(),mmd, value.getClass())).addAll(ownerOP, value, 0);
// Create a SCO wrapper with the elements loaded
replaceFieldWithWrapper(ownerOP, value, false, false);
}
else
{
if (mmd.getRelationType(ownerOP.getExecutionContext().getClassLoaderResolver()) == RelationType.MANY_TO_MANY_BI)
{
// Create a SCO wrapper, pass in null so it loads any from the datastore (on other side?)
replaceFieldWithWrapper(ownerOP, null, false, false);
}
else
{
// Create a SCO wrapper, pass in empty collection to avoid loading from DB (extra SQL)
replaceFieldWithWrapper(ownerOP, value, false, false);
}
}
}
}
}
/**
* Method to be called after any update of the owner class element.
* This method could be called in two situations
* <ul>
* <li>Update a collection field of an object by replacing the collection with a new collection,
* so UpdateRequest is called, which calls here</li>
* <li>Persist a new object, and it needed to wait til the element was inserted so
* goes into dirty state and then flush() triggers UpdateRequest, which comes here</li>
* </ul>
* @param ownerOP ObjectProvider of the owner
*/
public void postUpdate(ObjectProvider ownerOP)
{
ExecutionContext ec = ownerOP.getExecutionContext();
Collection value = (Collection) ownerOP.provideField(getAbsoluteFieldNumber());
if (containerIsStoredInSingleColumn())
{
// Make sure the elements are ok for proceeding
SCOUtils.validateObjectsForWriting(ec, value);
return;
}
if (value == null)
{
// remove any elements in the collection and replace it with an empty SCO wrapper
((CollectionStore) storeMgr.getBackingStoreForField(ec.getClassLoaderResolver(), mmd, null)).clear(ownerOP);
replaceFieldWithWrapper(ownerOP, null, false, false);
return;
}
if (value instanceof SCOContainer)
{
// Already have a SCO value
SCOContainer sco = (SCOContainer) value;
if (ownerOP.getObject() == sco.getOwner() && mmd.getName().equals(sco.getFieldName()))
{
// Flush any outstanding updates
ownerOP.getExecutionContext().flushOperationsForBackingStore(((BackedSCO)sco).getBackingStore(), ownerOP);
return;
}
if (sco.getOwner() != null)
{
throw new NucleusException(LOCALISER.msg("CollectionMapping.WrongOwnerError")).setFatal();
}
}
if (!mmd.isCascadeUpdate())
{
// TODO Should this throw exception if the element doesn't exist?
// User doesn't want to update by reachability
if (NucleusLogger.PERSISTENCE.isDebugEnabled())
{
NucleusLogger.PERSISTENCE.debug(LOCALISER.msg("007008", mmd.getFullFieldName()));
}
return;
}
if (NucleusLogger.PERSISTENCE.isDebugEnabled())
{
NucleusLogger.PERSISTENCE.debug(LOCALISER.msg("007009", mmd.getFullFieldName()));
}
CollectionStore backingStore = ((CollectionStore) storeMgr.getBackingStoreForField(
ec.getClassLoaderResolver(), mmd, value.getClass()));
backingStore.update(ownerOP, value);
// Replace the field with a wrapper containing these elements
replaceFieldWithWrapper(ownerOP, value, false, false);
}
/**
* Method to be called before any delete of the owner class element.
* @param ownerOP ObjectProvider of the owner
*/
public void preDelete(ObjectProvider ownerOP)
{
if (containerIsStoredInSingleColumn())
{
// Field is stored with the main object so nothing to clean up
return;
}
// makes sure field is loaded
ownerOP.isLoaded(getAbsoluteFieldNumber());
Collection value = (Collection) ownerOP.provideField(getAbsoluteFieldNumber());
if (value == null)
{
return;
}
boolean dependent = mmd.getCollection().isDependentElement();
if (mmd.isCascadeRemoveOrphans())
{
dependent = true;
}
boolean hasJoin = (mmd.getJoinMetaData() != null);
boolean hasFK = false;
if (!hasJoin)
{
if (mmd.getElementMetaData() != null && mmd.getElementMetaData().getForeignKeyMetaData() != null)
{
// FK collection, using <element> FK spec
hasFK = true;
}
else if (mmd.getForeignKeyMetaData() != null)
{
// FK collection, using <field> FK spec
hasFK = true;
}
AbstractMemberMetaData[] relatedMmds = mmd.getRelatedMemberMetaData(ownerOP.getExecutionContext().getClassLoaderResolver());
if (relatedMmds != null && relatedMmds[0].getForeignKeyMetaData() != null)
{
// FK collection (bidir), using <field> FK spec at other end
hasFK = true;
}
}
if (ownerOP.getExecutionContext().getStringProperty(PropertyNames.PROPERTY_DELETION_POLICY).equals("JDO2"))
{
// JDO2 doesn't currently (2.0 spec) take note of foreign-key
hasFK = false;
}
if (ownerOP.getExecutionContext().getManageRelations())
{
ownerOP.getExecutionContext().getRelationshipManager(ownerOP).relationChange(getAbsoluteFieldNumber(), value, null);
}
// TODO Why dont we just do clear here always ? THe backing store should take care of if nulling or deleting etc
if (dependent || hasJoin || !hasFK)
{
// Elements are either dependent (in which case we need to delete them) or theres a join (in which case
// we need to remove the join entries), or there are no FKs specified (in which case we need to clean up)
if (!(value instanceof SCO))
{
value = (Collection)ownerOP.wrapSCOField(getAbsoluteFieldNumber(), value, false, false, true);
}
value.clear();
// Flush any outstanding updates
ownerOP.getExecutionContext().flushOperationsForBackingStore(((BackedSCO)value).getBackingStore(), ownerOP);
}
}
}