在 GAE 上检索/记录多个实体的最便宜方法
我有一个具体的案例给我带来了一些疑问: 我的类 A 与 B 具有一对多关系:
A 1----->* B
据我所知,这使得它们的相关实例属于同一实体组。 我必须检索一个特定的 A 实例及其 B 实例之一,因此我搜索 A 并迭代其 B 列表以查找特定的 B(始终按 Id 搜索)。之后,我更改 A 和 B 的一个属性,并通过合并 A 来提交更改。所以这是我的问题:
- 知道我必须检索 A 和 B(因为我必须修改两者),我是否应该进行 2 次搜索而不是迭代B名单?
- 进行2次搜索,如果我只保留A,对B的修改会被保留吗?
- 在这种情况下,问题 1 和问题 2 会有相同的答案吗:
A 1----->* C 1----->* B
用一些代码补充问题:
@Entity
public class A
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Key key;
private int aField;
@OneToMany(cascade={CascadeType.ALL})
private List<B> bList;
//...
}
@Entity
public class B
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Key key;
private int someBField;
// no way to get A from here...
//...
}
public void someService1(Key aKey, Key bKey)
{
//...
// Here, I'm sure that bKey is of an entity son of aKey entity (I trust the client)
A a = entityManager.find(A.class, aKey);
a.setAField(a.getAField()++);
for(B b : a.getBList())
if(b.getKey().equals(bKey))
b.setSomeBField(b.getSomeBField()++);
entityManager.merge(a);
//...
}
public void someService2(Key aKey, Key bKey)
{
//...
// Here, I'm sure that bKey is of an entity son of aKey entity (I trust the client)
A a = entityManager.find(A.class, aKey);
a.setAField(a.getAField()++);
B b = entityManager.find(B.class, bKey);
b.setSomeBField(b.getSomeBField()++);
entityManager.merge(a);
//...
}
好吧,如果我做对了一切, someService1 和 someService2 都会做同样的事情。哪一个更好?
I have this specific case that bring me some doubts:
My class A have a one-to-many relationship with B:
A 1----->* B
Which, as far as I know, makes their related instances belong to the same entity group.
I have to retrieve one particular A instance and one of its B, so I search for A and iterate through its B List to find the particular B (always searching by Id). After that, I change one attribute of A and B and commit my changes by merging A. So here are my questions:
- Knowing that I must retrieve A and B (because I have to modify both), should I make 2 searches instead of iterating the B List?
- Making 2 searches, modifications on B will be persisted if I only persist A?
- Will question 1 and 2 have the same answer in this case:
A 1----->* C 1----->* B
Complementing the question with some code:
@Entity
public class A
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Key key;
private int aField;
@OneToMany(cascade={CascadeType.ALL})
private List<B> bList;
//...
}
@Entity
public class B
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Key key;
private int someBField;
// no way to get A from here...
//...
}
public void someService1(Key aKey, Key bKey)
{
//...
// Here, I'm sure that bKey is of an entity son of aKey entity (I trust the client)
A a = entityManager.find(A.class, aKey);
a.setAField(a.getAField()++);
for(B b : a.getBList())
if(b.getKey().equals(bKey))
b.setSomeBField(b.getSomeBField()++);
entityManager.merge(a);
//...
}
public void someService2(Key aKey, Key bKey)
{
//...
// Here, I'm sure that bKey is of an entity son of aKey entity (I trust the client)
A a = entityManager.find(A.class, aKey);
a.setAField(a.getAField()++);
B b = entityManager.find(B.class, bKey);
b.setSomeBField(b.getSomeBField()++);
entityManager.merge(a);
//...
}
Well, both someService1 and someService2 do the same thing, if I did everything right. Which one is better?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解正确,您不必迭代任何内容,而是使用
filter()
对您喜欢的任何Query
进行操作。示例 (Python):如果您使用 Java,则需要使用
addFilter
对您的查询 (更多信息)。If I understood correctly you don't have to iterate through anything, but instead use the
filter()
on anyQuery
you like. Examples (Python):If you're using Java the you'll have use
addFilter
on your queries (more info).如果您知道 id(并且您说您正在通过 id 进行搜索),则只需对 A 执行
em.find
操作,对 B 执行em.find
操作即可。 两个来电。遗憾的是,JPA API 没有em.findAll
。另一方面,JDO 确实有pm.getObjectsById
If you know the id (and you say you're searching by id) then just do an
em.find
for the A, andem.find
for the B. Two calls. Sadly the JPA API doesn't have aem.findAll
. JDO, on the other hand, does havepm.getObjectsById