1919
2020package org .apache .cayenne .access .flush ;
2121
22- import java .util .Map ;
23-
2422import org .apache .cayenne .CayenneRuntimeException ;
2523import org .apache .cayenne .ObjectId ;
2624import org .apache .cayenne .access .DataDomain ;
3735import org .apache .cayenne .map .ObjEntity ;
3836import org .apache .cayenne .reflect .ClassDescriptor ;
3937
38+ import java .util .Map ;
39+
4040/**
4141 * Visitor that fills replacement map of {@link ObjectId}s of inserted objects.
4242 *
@@ -65,9 +65,9 @@ public Void visitInsert(InsertDbRowOp dbRow) {
6565 return null ;
6666 }
6767
68- if ((lastObjEntity == null && lastDbEntity == null ) || !id .getEntityName ().equals (lastEntityName )) {
68+ if ((lastObjEntity == null && lastDbEntity == null ) || !id .getEntityName ().equals (lastEntityName )) {
6969 lastEntityName = id .getEntityName ();
70- if (lastEntityName .startsWith (ASTDbPath .DB_PREFIX )) {
70+ if (lastEntityName .startsWith (ASTDbPath .DB_PREFIX )) {
7171 lastDbEntity = resolver .getDbEntity (lastEntityName .substring (ASTDbPath .DB_PREFIX .length ()));
7272 lastObjEntity = null ;
7373 lastDescriptor = null ;
@@ -87,7 +87,7 @@ public Void visitInsert(InsertDbRowOp dbRow) {
8787 @ Override
8888 public Void visitDelete (DeleteDbRowOp dbRow ) {
8989 // flattened ids will be temporary for delete operation, replace it
90- if (dbRow .getChangeId ().isTemporary () && dbRow .getChangeId ().isReplacementIdAttached ()) {
90+ if (dbRow .getChangeId ().isTemporary () && dbRow .getChangeId ().isReplacementIdAttached ()) {
9191 dbRow .setChangeId (dbRow .getChangeId ().createReplacementId ());
9292 }
9393 return null ;
@@ -110,18 +110,17 @@ private void createPermanentId(InsertDbRowOp dbRow) {
110110 continue ;
111111 }
112112
113+ ObjAttribute objAttr = lastObjEntity != null ? lastObjEntity .getAttributeForDbAttribute (dbAttr ) : null ;
114+
113115 // handle meaningful PK
114- if (lastObjEntity != null ) {
115- ObjAttribute objAttr = lastObjEntity .getAttributeForDbAttribute (dbAttr );
116- if (objAttr != null ) {
117- Object value = lastDescriptor .getProperty (objAttr .getName ()).readPropertyDirectly (dbRow .getObject ());
118- if (value != null ) {
119- // primitive 0 has to be treated as NULL, or otherwise we can't generate PK for POJO's
120- Class <?> javaClass = objAttr .getJavaClass ();
121- if (!javaClass .isPrimitive () || !(value instanceof Number number ) || number .intValue () != 0 ) {
122- idMap .put (dbAttrName , value );
123- continue ;
124- }
116+ if (objAttr != null ) {
117+ Object value = lastDescriptor .getProperty (objAttr .getName ()).readPropertyDirectly (dbRow .getObject ());
118+ if (value != null ) {
119+ // primitive 0 has to be treated as NULL, or otherwise we can't generate PK for POJO's
120+ Class <?> javaClass = objAttr .getJavaClass ();
121+ if (!javaClass .isPrimitive () || !(value instanceof Number number ) || number .intValue () != 0 ) {
122+ idMap .put (dbAttrName , value );
123+ continue ;
125124 }
126125 }
127126 }
@@ -140,11 +139,13 @@ private void createPermanentId(InsertDbRowOp dbRow) {
140139
141140 // finally, use database generation mechanism
142141 try {
143- Object pkValue = pkGenerator .generatePk (lastNode , dbAttr );
142+ Class <?> javaType = objAttr != null ? objAttr .getJavaClass () : null ;
143+
144+ Object pkValue = pkGenerator .generatePk (lastNode , dbAttr , javaType );
144145 idMap .put (dbAttrName , pkValue );
145146 autoPkDone = true ;
146147 } catch (Exception ex ) {
147- throw new CayenneRuntimeException ("Error generating PK: %s" , ex , ex .getMessage ());
148+ throw new CayenneRuntimeException ("Error generating PK: %s" , ex , ex .getMessage ());
148149 }
149150 }
150151 }
0 commit comments