实施无主关系 Google App Engine

发布于 2024-07-30 03:40:05 字数 626 浏览 6 评论 0原文

我的问题更多的是关于如何与 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 技术交流群。

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

发布评论

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

评论(2

叫思念不要吵 2024-08-06 03:40:05

理想情况下,User 对象应该有一个返回 List 的方法,以便调用者获得干净的 API。 实现此目的的一种方法是让 User 对象拥有 DAO 的实例,以便它可以要求 DAO 执行查询。

为此,PersistenceManager 在请求结束之前无法关闭。 一种方法是创建一个 servlet 过滤器

public class PersistenceManagerFilter implements Filter { 
  public void init(FilterConfig filterConfig) throws ServletException {
  }

  public void destroy() {
  }

  public void doFilter(ServletRequest request, ServletResponse response,
      FilterChain chain)  throws IOException, ServletException {
    PersistenceManager pm = PMF.get().getPersistenceManager();
    try {
      request.setAttribute("pm", pm);
      chain.doFilter(request, response);
    } finally {
      pm.close();
    }
  }
}

然后你可以注入将 PersistenceManager 添加到您的 DAO 中。 如果您使用 Guice 您可以这样做:

@RequestScoped
public class UserDao {
  private final PersistenceManager pm;

  @Inject
  UserDao(PersistenceManager pm) {
    this.pm = pm;
  }

  public User getUser(parameters) {
    Key key = createKey(parameters);
    User user = pm.getObjectById(User.class, key);
    user.setUserDao(this);
    return user;
  }
}

UserDao< 时效果最佳/code> 与 User 位于同一包中,因此 User.setUserDao() 可以是包范围。 您可以决定将 PersistenceManager 设置到 User 对象中,而不是 UserDao 中。

接下来,您需要创建一个 Guice 模块来绑定 PersistenceManager

public class PersistenceManagerModule extends AbstractModule {
  @Override 
  protected void configure() {
  }

  @Provides @RequestScoped
  PersistenceManager providePersistenceManager(HttpServletRequest request) {
    return (PersistenceManager) request.getAttribute("pm");
  }
}

最后您需要配置 Google App Engine 使用 Guice

这只是一种可能的方法。 如果让 providePersistenceManager 创建 PersistenceManager 并将其存储为请求属性,而不是让 servlet 过滤器创建 PersistenceManager (过滤器仍会关闭它)。 您还可以让 servlet 过滤器获取 Guice Injector,这样您就可以避免使用 setAttribute()getAttribute(),而是使用类型更安全的 API。

Ideally, the User object would have a method that returns a List<UnownedObject> so that the callers get a clean API. One way to do that is for the User 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:

public class PersistenceManagerFilter implements Filter { 
  public void init(FilterConfig filterConfig) throws ServletException {
  }

  public void destroy() {
  }

  public void doFilter(ServletRequest request, ServletResponse response,
      FilterChain chain)  throws IOException, ServletException {
    PersistenceManager pm = PMF.get().getPersistenceManager();
    try {
      request.setAttribute("pm", pm);
      chain.doFilter(request, response);
    } finally {
      pm.close();
    }
  }
}

Then you can injected the PersistenceManager into your DAO. If you use Guice you can do:

@RequestScoped
public class UserDao {
  private final PersistenceManager pm;

  @Inject
  UserDao(PersistenceManager pm) {
    this.pm = pm;
  }

  public User getUser(parameters) {
    Key key = createKey(parameters);
    User user = pm.getObjectById(User.class, key);
    user.setUserDao(this);
    return user;
  }
}

This works best when UserDao is in the same package as User so User.setUserDao() can be package scope. You could decide to set the PersistenceManager into the User object instead of the UserDao.

Next you need to create a Guice module to bind PersistenceManager:

public class PersistenceManagerModule extends AbstractModule {
  @Override 
  protected void configure() {
  }

  @Provides @RequestScoped
  PersistenceManager providePersistenceManager(HttpServletRequest request) {
    return (PersistenceManager) request.getAttribute("pm");
  }
}

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 the PersistenceManager and store it as a request attribute instead of having the servlet filter create the PersistenceManager (the filter would still close it). You could also have the servlet filter get the Guice Injector so you can avoid using setAttribute() and getAttribute() and instead use a more type-safe API.

时光暖心i 2024-08-06 03:40:05

Google 建议创建单例 PersistenceManagerFactory。 我不会在你的模型中添加 PMF。

Google recommends creating a singleton PersistenceManagerFactory. I wouldn't stick a PMF in your model.

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