如何使用JDO(DataNucleus)进行更新&删除数据?

发布于 2024-09-16 18:27:56 字数 1440 浏览 6 评论 0原文

我使用 apache.JDO /w DataNucleus 设置了一个小项目。我可以毫无问题地保存数据,但在尝试更新或删除它们时遇到了困难。

场景如下:

  1. 我创建一个对象并创建一个对象。持久化它,它获取和 id
     @PrimaryKey  
     @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)  
     private Long id;  
  1. 我关闭 PersistenceManager
  2. 在应用程序中我修改我的对象(瞬态)
  3. 我尝试再次持久化(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:

  1. I create an object & persist it, it gets and id
     @PrimaryKey  
     @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)  
     private Long id;  
  1. I close the PersistenceManager
  2. In the app I modify my object (Transient)
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我家小可爱 2024-09-23 18:27:56

我已经解决了我的问题(@point 2)

pm = PMF.getPM();
dao = pm.getObjectById(DO.class, 1L);
dao.setValue("B");
pm.makePersistent(dao);
pm.close();

但是如果我有 70-100 个字段,这个解决方案会有点昂贵,因为我必须单独设置每个字段。

我没有手动完成,而是通过反射完成的 - 那么 DataNucleus 相对于 Hibernate 的优势是什么? - (据我所知)它也使用运行时内省。

如果我错了,请纠正我 - 我仍然是这个领域的新手......但是:)

I've solved my problem (@point 2)

pm = PMF.getPM();
dao = pm.getObjectById(DO.class, 1L);
dao.setValue("B");
pm.makePersistent(dao);
pm.close();

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 :)

怪我太投入 2024-09-23 18:27:56

您不需要再次调用 makePersistent。

long id = objectId; //Id of the object you want to update.

pm = PMF.getPM();

DO dao = pm.getObjectById(DO.class, id);
dao.setValue("B");

pm.close();

要删除,请执行以下操作;

long id = objectId; //Id of the object you want to delete.
pm = PMF.getPM();

DO dao = pm.getObjectById(DO.class, id);
pm.deletePersistent(dao);

pm.close();

您没有使用事务来提交。您可以查看 DataNucleus 文档

You don't need to call makePersistent agian.

long id = objectId; //Id of the object you want to update.

pm = PMF.getPM();

DO dao = pm.getObjectById(DO.class, id);
dao.setValue("B");

pm.close();

To delete do this;

long id = objectId; //Id of the object you want to delete.
pm = PMF.getPM();

DO dao = pm.getObjectById(DO.class, id);
pm.deletePersistent(dao);

pm.close();

You are not using transaction to commit. You can look at DataNucleus docs

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文