// In a unit of work/writeObjects the preInsert may have caused a shallow insert of this object,
// in this case this second write must do an update.
if (commitManager.isShallowCommitted(object)) {
updateForeignKeyFieldAfterInsert();
} else {
AbstractRecord modifyRow = writeQuery.getModifyRow();
if (modifyRow == null) {// Maybe have been passed in as in aggregate collection.
if (writeQuery.shouldCascadeParts()) {
writeQuery.setModifyRow(descriptor.getObjectBuilder().buildRow(object, session));
} else {
writeQuery.setModifyRow(descriptor.getObjectBuilder().buildRowForShallowInsert(object, session));
}
} else {
if (writeQuery.shouldCascadeParts()) {
writeQuery.setModifyRow(descriptor.getObjectBuilder().buildRow(modifyRow, object, session));
} else {
writeQuery.setModifyRow(descriptor.getObjectBuilder().buildRowForShallowInsert(modifyRow, object, session));
}
}
modifyRow = getModifyRow();
// the modify row and the translation row are the same for insert
writeQuery.setTranslationRow(modifyRow);
if (!descriptor.isAggregateCollectionDescriptor()) {// Should/cannot be recomputed in aggregate collection.
writeQuery.setPrimaryKey(descriptor.getObjectBuilder().extractPrimaryKeyFromObject(object, session));
}
addWriteLockFieldForInsert();
// CR#3237
// Store the size of the modify row so we can determine if the user has added to the row in the insert.
int modifyRowSize = modifyRow.size();
// PERF: Avoid events if no listeners.
if (eventManager.hasAnyEventListeners()) {
DescriptorEvent event = new DescriptorEvent(DescriptorEventManager.AboutToInsertEvent, writeQuery);
event.setRecord(modifyRow);
eventManager.executeEvent(event);
}
if (QueryMonitor.shouldMonitor()) {
QueryMonitor.incrementInsert(writeQuery);
}
// CR#3237
// Call insert with a boolean that tells it to reprepare if the user has altered the modify row.
insertObject(modifyRowSize != modifyRow.size());
// register the object before post insert to resolve possible cycles
registerObjectInIdentityMap(object, descriptor, session);
}