final boolean isInsert, final InsertMappingConsumer consumer) {
StoreRelationEvent event = new StoreRelationEvent() {
public void apply() {
DatastoreTable table = fieldManager.getDatastoreTable();
StateManager sm = getStateManager();
int fieldNumber = ammd.getAbsoluteFieldNumber();
// Based on ParameterSetter
try {
JavaTypeMapping mapping = table.getMemberMappingInDatastoreClass(ammd);
if (mapping instanceof EmbeddedPCMapping ||
mapping instanceof SerialisedPCMapping ||
mapping instanceof SerialisedReferenceMapping ||
mapping instanceof PersistenceCapableMapping ||
mapping instanceof InterfaceMapping) {
setObjectViaMapping(mapping, table, value, sm, fieldNumber);
sm.wrapSCOField(fieldNumber, value, false, true, true);
} else {
if (isInsert) {
runPostInsertMappingCallbacks(consumer, value);
} else {
runPostUpdateMappingCallbacks(consumer);
}
}
} catch (NotYetFlushedException e) {
if (acmd.getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber).getNullValue() == NullValue.EXCEPTION) {
throw e;
}
sm.updateFieldAfterInsert(e.getPersistable(), fieldNumber);
}
}
private void setObjectViaMapping(JavaTypeMapping mapping, DatastoreTable table, Object value,
StateManager sm, int fieldNumber) {
boolean fieldIsParentKeyProvider = table.isParentKeyProvider(ammd);
if (!fieldIsParentKeyProvider) {
checkForParentSwitch(value, sm);
}
Entity entity = fieldManager.getEntity();
mapping.setObject(
fieldManager.getObjectManager(),
entity,
fieldIsParentKeyProvider ? IS_PARENT_VALUE_ARR : IS_FK_VALUE_ARR,
value,
sm,
fieldNumber);
// If the field we're setting is the one side of an owned many-to-one,
// its pk needs to be the parent of the key of the entity we're
// currently populating. We look for a magic property that tells
// us if this change needs to be made. See
// DatastoreFKMapping.setObject for all the gory details.
Object parentKeyObj = entity.getProperty(PARENT_KEY_PROPERTY);
if (parentKeyObj != null) {
AbstractClassMetaData parentCmd = sm.getMetaDataManager().getMetaDataForClass(
ammd.getType(), fieldManager.getClassLoaderResolver());
Key parentKey = EntityUtils.getPkAsKey(parentKeyObj, parentCmd, fieldManager.getObjectManager());
entity.removeProperty(PARENT_KEY_PROPERTY);
fieldManager.recreateEntityWithParent(parentKey);
}