DbEntity entity = descriptor.getDbEntity();
DataNode node = parent.getDomain().lookupDataNode(entity.getDataMap());
boolean supportsGeneratedKeys = node.getAdapter().supportsGeneratedKeys();
PkGenerator pkGenerator = node.getAdapter().getPkGenerator();
for (Persistent object : objects) {
ObjectId id = object.getObjectId();
if (id == null || !id.isTemporary()) {
continue;
}
// modify replacement id directly...
Map<String, Object> idMap = id.getReplacementIdMap();
boolean autoPkDone = false;
for (DbAttribute dbAttr : entity.getPrimaryKeys()) {
String dbAttrName = dbAttr.getName();
if (idMap.containsKey(dbAttrName)) {
continue;
}
// handle meaningful PK
ObjAttribute objAttr = objEntity.getAttributeForDbAttribute(dbAttr);
if (objAttr != null) {
Object value = descriptor.getClassDescriptor().getProperty(
objAttr.getName()).readPropertyDirectly(object);
if (value != null) {
Class<?> javaClass = objAttr.getJavaClass();
if (javaClass.isPrimitive()
&& value instanceof Number
&& ((Number) value).intValue() == 0) {
// primitive 0 has to be treated as NULL, or otherwise we
// can't generate PK for POJO's
}
else {
idMap.put(dbAttrName, value);
continue;
}
}
}
// skip db-generated
if (supportsGeneratedKeys && dbAttr.isGenerated()) {
continue;
}
// skip propagated
if (isPropagated(dbAttr)) {
continue;
}
// only a single key can be generated from DB... if this is done already
// in this loop, we must bail out.
if (autoPkDone) {
throw new CayenneRuntimeException(
"Primary Key autogeneration only works for a single attribute.");
}
// finally, use database generation mechanism
try {
Object pkValue = pkGenerator.generatePk(node, dbAttr);
idMap.put(dbAttrName, pkValue);
autoPkDone = true;
}
catch (Exception ex) {
throw new CayenneRuntimeException("Error generating PK: "