Datanucleus JDO检索新生成的主键 [英] Datanucleus JDO Retrieve newly generated primary key

查看:160
本文介绍了Datanucleus JDO检索新生成的主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在GWT项目中使用datanucleus和jdo。在使用makePersistent()向数据库添加元素后,如何检索生成的主键


$ b 编辑
我们使用注释,这里是主键:

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

我不是那个做过映射的人,我还没有完全掌握这一点。 / p>

解决方案

对象的关键字应该在持久保存时自动设置:

  MyObject obj = new MyObject(); 
Long id = obj.getId(); //错误!仍然是空的。
pm.makePersistent(obj);
id = obj.getId(); //正确。


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.

解决方案

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.

这篇关于Datanucleus JDO检索新生成的主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆