Datanucleus JDO Retrieve newly generated primary key

守給你的承諾、 提交于 2019-12-12 01:35:43

问题


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.


来源:https://stackoverflow.com/questions/3021297/datanucleus-jdo-retrieve-newly-generated-primary-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!