JPA - Returning an auto generated id after persist()

前端 未结 7 1466
耶瑟儿~
耶瑟儿~ 2020-11-28 02:14

I\'m using JPA (EclipseLink) and Spring. Say I have a simple entity with an auto-generated ID:

@Entity
public class ABC implements Serializable {
     @Id
           


        
7条回答
  •  无人及你
    2020-11-28 03:05

    Another option compatible to 4.0:

    Before committing the changes, you can recover the new CayenneDataObject object(s) from the collection associated to the context, like this:

    CayenneDataObject dataObjectsCollection = (CayenneDataObject)cayenneContext.newObjects();
    

    then access the ObjectId for each one in the collection, like:

    ObjectId objectId = dataObject.getObjectId();
    

    Finally you can iterate under the values, where usually the generated-id is going to be the first one of the values (for a single column key) in the Map returned by getIdSnapshot(), it contains also the column name(s) associated to the PK as key(s):

    objectId.getIdSnapshot().values()
    

提交回复
热议问题