if (!sm.isNew() || sm.isFlushed() || sm.isDeleted())
return false;
// special-case oid fields, which require us to look inside the oid
// object
FieldMetaData fmd = _meta.getField(field);
if (fmd.getDeclaredTypeCode() == JavaTypes.OID) {
// try to shortcut if possible
if (_oid != null || isEmbedded() || !isPersistent())
return true;
// check embedded fields of oid for value strategy + default value
FieldMetaData[] pks = fmd.getEmbeddedMetaData().getFields();
OpenJPAStateManager oidsm = null;
boolean assign = false;
for (int i = 0; !assign && i < pks.length; i++) {
if (pks[i].getValueStrategy() == ValueStrategies.NONE)
continue;
if (oidsm == null)
oidsm = new ObjectIdStateManager(fetchObjectField(field),
this, fmd);
assign = oidsm.isDefaultValue(i);
}
return assign && assignObjectId(!preFlushing, preFlushing);
}
// Just return if there's no value generation strategy
if (fmd.getValueStrategy() == ValueStrategies.NONE)
return false;
// Throw exception if field already has a value assigned.
// @GeneratedValue overrides POJO initial values and setter methods
if (!fmd.isValueGenerated() && !isDefaultValue(field))
throw new InvalidStateException(_loc.get(
"existing-value-override-excep", fmd.getFullName(false)));
// for primary key fields, assign the object id and recache so that
// to the user, so it looks like the oid always matches the pk fields
if (fmd.isPrimaryKey() && !isEmbedded())
return assignObjectId(!preFlushing, preFlushing);
// for other fields just assign the field or flush if needed
if (_broker.getStoreManager().assignField(this, field, preFlushing)) {
fmd.setValueGenerated(true);
return true;
}
if (!preFlushing)
_broker.flush();
return !preFlushing;