如何使用JDO(DataNucleus)进行更新&删除数据?
我使用 apache.JDO /w DataNucleus 设置了一个小项目。我可以毫无问题地保存数据,但在尝试更新或删除它们时遇到了困难。
场景如下:
- 我创建一个对象并创建一个对象。持久化它,它获取和 id
@PrimaryKey @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) private Long id;
- 我关闭 PersistenceManager
- 在应用程序中我修改我的对象(瞬态)
- 我尝试再次持久化(id 字段相同),但它不会更新它会导致在
Google 中 创建一个新对象App Engine 相同的场景给了我一个更新(预期结果 - 见下文)。
我还将给您一个小代码示例来举例说明我的问题:
PersistenceManager pm = PMF.getPM(); Option dao = new Option(String.class, "field", "A"); pm.makePersistent(dao); pm.close(); System.out.println("1"); for (Object o : Model.findAll(Option.class)) System.out.println(((Option) o).getValue()); dao.setValue("B"); pm = PMF.getPM(); pm.makePersistent(dao); pm.close(); System.out.println("2"); for (Object o : Model.findAll(Option.class)) System.out.println(((Option) o).getValue()); pm = PMF.getPM(); pm.makePersistent(dao); pm.deletePersistent(dao); pm.close(); System.out.println("3"); for (Object o : Model.findAll(Option.class)) System.out.println(((Option) o).getValue());
我希望输出为:
1 A 2 B 3
但它却给了我:
1 A 2 A B 3 A B
关于我做错了什么的任何建议? (顺便说一句,我使用非事务性 RW,并启用了 RetainValues)
I've set up a small project using apache.JDO /w DataNucleus. I can save data w/o any problems, but I got stuck when trying to update or delete them.
The scenario is the following:
- I create an object & persist it, it gets and id
@PrimaryKey @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) private Long id;
- I close the PersistenceManager
- In the app I modify my object (Transient)
- I try to persist again (the id field is the same), but instead of update it results in the creation of a new object
In Google App Engine the same scenario gave me an update (the expected results - see below).
I will also give you a small code sample to exemplify my problem:
PersistenceManager pm = PMF.getPM(); Option dao = new Option(String.class, "field", "A"); pm.makePersistent(dao); pm.close(); System.out.println("1"); for (Object o : Model.findAll(Option.class)) System.out.println(((Option) o).getValue()); dao.setValue("B"); pm = PMF.getPM(); pm.makePersistent(dao); pm.close(); System.out.println("2"); for (Object o : Model.findAll(Option.class)) System.out.println(((Option) o).getValue()); pm = PMF.getPM(); pm.makePersistent(dao); pm.deletePersistent(dao); pm.close(); System.out.println("3"); for (Object o : Model.findAll(Option.class)) System.out.println(((Option) o).getValue());
I would expect the output to be:
1 A 2 B 3
But instead it gives me:
1 A 2 A B 3 A B
Any suggestions on what am I doing wrong?
(btw I use non-transactional RW, with RetainValues enabled)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经解决了我的问题(@point 2)
但是如果我有 70-100 个字段,这个解决方案会有点昂贵,因为我必须单独设置每个字段。
我没有手动完成,而是通过反射完成的 - 那么 DataNucleus 相对于 Hibernate 的优势是什么? - (据我所知)它也使用运行时内省。
如果我错了,请纠正我 - 我仍然是这个领域的新手......但是:)
I've solved my problem (@point 2)
But this solutions is somewhat costly if I have 70-100 fields, because I have to set each separately.
I haven't done it manually, but with reflection - but then what's the advantage of DataNucleus over Hibernate? - which (as far as I know) also uses runtime introspection.
Please correct me if I'm wrong - I'm still a newbie in this area... yet :)
您不需要再次调用 makePersistent。
要删除,请执行以下操作;
您没有使用事务来提交。您可以查看 DataNucleus 文档
You don't need to call
makePersistent
agian.To delete do this;
You are not using transaction to commit. You can look at DataNucleus docs