实施无主关系 Google App Engine
我的问题更多的是关于如何与 Google App Engine 实现无主关系的最佳实践问题。 我正在使用 JDO 来进行持久化,并且像 google 文档中推荐的那样,我正在持久化我的无主关系列表,如下所示:
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class User implements Serializable, UserDetails {
...
@Persistent
private List<Key> groups;
...
}
现在,当我使用 Key 对象查询该对象列表时,我遇到了困境。 因此,当我获取组键列表以便实际返回组对象列表时,我必须查找该键才能获取对象。 我的问题是对模型对象进行无主查找的推荐方法是什么?
我的 Model 对象上是否应该有一个 PersistanceManagerFactory 实例,以便我可以进行查找?
我是否应该在 Model 对象上有一个 GroupDAO 对象的实例,以便我可以进行查找?
我应该有一个实用程序来执行此类查找吗?
我对此很陌生,所以我只想知道哪种方法是最好的方法。 谢谢。
My question is more of a best practices question on how to implement unowned relationships with Google App Engine. I am using JDO to do my persistence and like recommended in the google docs I'm persisting my list of unowned relationships like so:
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class User implements Serializable, UserDetails {
...
@Persistent
private List<Key> groups;
...
}
Now I came across my predicament when I went to query that list of objects using they Key object. So when I get my list of group keys in order to actually return a list of Group objects I have to do a look up on that key to get the object. My question is what is the recommended way of doing a unowned look up on a model object?
Should I have an instance of the PersistanceManagerFactory on my Model object so I can do a lookup?
Should I have an instance of my GroupDAO object on my Model object so I can do a look up?
Should I have a Utility to do this type of lookup?
I'm new to this so I just want to know which is the best way to do this. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
理想情况下,
User
对象应该有一个返回List
的方法,以便调用者获得干净的 API。 实现此目的的一种方法是让User
对象拥有 DAO 的实例,以便它可以要求 DAO 执行查询。为此,
PersistenceManager
在请求结束之前无法关闭。 一种方法是创建一个 servlet 过滤器:然后你可以注入将
PersistenceManager
添加到您的 DAO 中。 如果您使用 Guice 您可以这样做:当
UserDao< 时效果最佳/code> 与
User
位于同一包中,因此User.setUserDao()
可以是包范围。 您可以决定将PersistenceManager
设置到User
对象中,而不是UserDao
中。接下来,您需要创建一个 Guice 模块来绑定
PersistenceManager
:最后您需要配置 Google App Engine 使用 Guice。
这只是一种可能的方法。 如果让
providePersistenceManager
创建PersistenceManager
并将其存储为请求属性,而不是让 servlet 过滤器创建PersistenceManager
(过滤器仍会关闭它)。 您还可以让 servlet 过滤器获取 Guice Injector,这样您就可以避免使用setAttribute()
和getAttribute()
,而是使用类型更安全的 API。Ideally, the
User
object would have a method that returns aList<UnownedObject>
so that the callers get a clean API. One way to do that is for theUser
object to have an instance of the DAO so it could ask the DAO to do the query.In order to do this, the
PersistenceManager
can't be closed until the end of the request. One way to do this is to create a servlet filter:Then you can injected the
PersistenceManager
into your DAO. If you use Guice you can do:This works best when
UserDao
is in the same package asUser
soUser.setUserDao()
can be package scope. You could decide to set thePersistenceManager
into theUser
object instead of theUserDao
.Next you need to create a Guice module to bind
PersistenceManager
:Finally you need to configure Google App Engine to use Guice.
This is just one possible way to do it. You could be more clever had have
providePersistenceManager
create thePersistenceManager
and store it as a request attribute instead of having the servlet filter create thePersistenceManager
(the filter would still close it). You could also have the servlet filter get the Guice Injector so you can avoid usingsetAttribute()
andgetAttribute()
and instead use a more type-safe API.Google 建议创建单例 PersistenceManagerFactory。 我不会在你的模型中添加 PMF。
Google recommends creating a singleton PersistenceManagerFactory. I wouldn't stick a PMF in your model.