Datanucleus JDO Retrieve newly generated primary k

2019-08-31 04:39发布

问题:

I am using datanucleus and jdo in a GWT project. How can I retrieve the generated primary key after adding an element to the database with makePersistent()

Edit We use annotations, here is the primary key:

    @PrimaryKey
@Column(name = "id_bla", allowsNull = "false")
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY, extensions = { @Extension(vendorName = "datanucleus", key = "strategy-when-notnull", value = "false") })
private Long idBla;

I am not the one who did the mapping and I don't get all of this yet.

回答1:

The object's key should be automatically set when it is persisted:

MyObject obj = new MyObject();
Long id = obj.getId();  // WRONG! Will still be null.
pm.makePersistent(obj);
id = obj.getId();  // Correct.