如何在一个实体组中创建实体?

发布于 2024-08-20 04:19:04 字数 225 浏览 5 评论 0原文

我正在构建一个基于谷歌应用程序引擎(Java)的应用程序,使用 JDO 进行持久化。

有人可以给我一个例子或者给我指出一些代码,这些代码显示在事务中使用 javax.jdo.PersistenceManager.makePersistenceAll() 持久化多个实体(相同类型)。

基本上我需要了解如何将多个实体放入一个实体组中,以便可以在事务内使用 makePersistentAll() 保存它们。

I am building an app based on google app engine (Java) using JDO for persistence.

Can someone give me an example or a point me to some code which shows persisting of multiple entities (of same type) using javax.jdo.PersistenceManager.makePersistentAll() within a transaction.

Basically I need to understand how to put multiple entites in one Entity Group so that they can be saved using makePersistentAll() inside transaction.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

逆蝶 2024-08-27 04:19:04

本文档的这一部分正是针对此问题进行处理的。

This section of the docs deals with exactly that.

弱骨蛰伏 2024-08-27 04:19:04

我这样做了:

public static final Key root_key = KeyFactory.createKey("Object", "RootKey");

...

所以一个典型的数据存储持久对象将在构造函数中设置 id,而不是自动获取 id

public DSO_MyType(string Name, Key parent)
    {
        KeyFactory.Builder b = new KeyFactory.Builder(parent);;
        id = b.addChild(DSO_MyType.class.getSimpleName() , Name).getKey();
    }

,并且您将 root_key 作为父级传递,

我不确定是否可以将不同的父级传递给同类对象

i did this:

public static final Key root_key = KeyFactory.createKey("Object", "RootKey");

...

so a typical datastore persistent object will set the id in the constructor instead of getting one automatically

public DSO_MyType(string Name, Key parent)
    {
        KeyFactory.Builder b = new KeyFactory.Builder(parent);;
        id = b.addChild(DSO_MyType.class.getSimpleName() , Name).getKey();
    }

and you pass root_key as the parent

i'm not sure if you can pass different parents to objects of the same kind

青衫儰鉨ミ守葔 2024-08-27 04:19:04

感谢尼克的回复。

本文档仅介绍应用程序引擎在存在父子关系时对实体组的隐式处理。我想在事务中使用 PeristentManager.makePersistentAll(list) 保存多个相同类型的对象。如果对象不是同一实体组,则会引发异常。目前我可以按如下方式执行此操作,但认为必须有一个更好、更合适的方法来执行此操作 -

User u1 = new User("a");
UserDAO.getInstance().addObject(user1); 
// UserDAO.addObject uses PersistentManager.makePersistent() in transaction and user 
// object now has its Key set. I want to avoid this step.

User u2 = new User("x"); 
u2.setKey(KeyFactory.createKey(u1.getKey(),User.class.getSimpleName(), 100 /*some random id*/)); 

User u3 = new User("p");
u3.setKey(KeyFactory.createKey(u1.getKey(), User.class.getSimpleName(), 200)); 

UserDAO.getInstance().addObjects(Arrays.asList(new User[]{u2, u3})); 
// UserDAO.addObjects uses PersistentManager.makePersistentAll() in transaction.

尽管此方法有效,但问题在于您必须依赖于已经持久的实体来创建实体组。

Thanks for the response Nick.

This document only tells about implicit handling of entity groups by app engine when its a parent-child relationship. I want to save multiple objects of same type using PeristentManager.makePersistentAll(list) within a transaction. If objects are not same Entity Group this throws exception. Currently I could do it as below but think there must be a better and more appropriate approach to do this -

User u1 = new User("a");
UserDAO.getInstance().addObject(user1); 
// UserDAO.addObject uses PersistentManager.makePersistent() in transaction and user 
// object now has its Key set. I want to avoid this step.

User u2 = new User("x"); 
u2.setKey(KeyFactory.createKey(u1.getKey(),User.class.getSimpleName(), 100 /*some random id*/)); 

User u3 = new User("p");
u3.setKey(KeyFactory.createKey(u1.getKey(), User.class.getSimpleName(), 200)); 

UserDAO.getInstance().addObjects(Arrays.asList(new User[]{u2, u3})); 
// UserDAO.addObjects uses PersistentManager.makePersistentAll() in transaction.

Although this approach works, the problem with this is that you have to depend on an already persistent entity to create an entity group.

第几種人 2024-08-27 04:19:04

Gopi,据我所知,您不必这样做...这应该有效(尚未测试):

List<User> userList = new ArrayList<User>();
userList.add(new User("a"));
userList.add(new User("b"));
userList.add(new User("c"));
UserDAO().getInstance().addObjects(userList);

同样,据我所知,这应该将所有这些对象放在同一个实体组中。我很想知道我是否错了。

Gopi, AFAIK you don't have to do that... this should work (haven't tested it):

List<User> userList = new ArrayList<User>();
userList.add(new User("a"));
userList.add(new User("b"));
userList.add(new User("c"));
UserDAO().getInstance().addObjects(userList);

Again, AFAIK, this should put all these objects in the same entity group. I'd love to know if I am wrong.

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