ObjEntity objEntity = descriptor.getEntity();
DbEntity dbEntity = objEntity.getDbEntity();
DataNode node = parent.getDomain().lookupDataNode(dbEntity.getDataMap());
boolean supportsGeneratedKeys = node.getAdapter().supportsGeneratedKeys();
PkGenerator pkGenerator = node.getAdapter().getPkGenerator();
Iterator i = dataObjects.iterator();
while (i.hasNext()) {
Persistent object = (Persistent) i.next();
ObjectId id = object.getObjectId();
if (id == null || !id.isTemporary()) {
continue;
}
// modify replacement id directly...
Map idMap = id.getReplacementIdMap();
boolean autoPkDone = false;
Iterator it = dbEntity.getPrimaryKey().iterator();
while (it.hasNext()) {
DbAttribute dbAttr = (DbAttribute) it.next();
String dbAttrName = dbAttr.getName();
if (idMap.containsKey(dbAttrName)) {
continue;
}
// handle meaningful PK
ObjAttribute objAttr = objEntity.getAttributeForDbAttribute(dbAttr);
if (objAttr != null) {
Object value = descriptor
.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.generatePkForDbEntity(node, dbEntity);
idMap.put(dbAttrName, pkValue);
autoPkDone = true;
}
catch (Exception ex) {
throw new CayenneRuntimeException("Error generating PK: "