Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ideally, the <code>User</code> object would have a method that returns a <code>List&lt;UnownedObject&gt;</code> so that the callers get a clean API. One way to do that is for the <code>User</code> object to have an instance of the DAO so it could ask the DAO to do the query.</p> <p>In order to do this, the <code>PersistenceManager</code> can't be closed until the end of the request. One way to do this is to create a <a href="http://java.sun.com/products/servlet/Filters.html" rel="nofollow noreferrer">servlet filter</a>:</p> <pre><code>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(); } } } </code></pre> <p>Then you can injected the <code>PersistenceManager</code> into your DAO. If you use <a href="http://code.google.com/p/google-guice/" rel="nofollow noreferrer">Guice</a> you can do:</p> <pre><code>@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; } } </code></pre> <p>This works best when <code>UserDao</code> is in the same package as <code>User</code> so <code>User.setUserDao()</code> can be package scope. You could decide to set the <code>PersistenceManager</code> into the <code>User</code> object instead of the <code>UserDao</code>.</p> <p>Next you need to create a Guice module to bind <code>PersistenceManager</code>:</p> <pre><code>public class PersistenceManagerModule extends AbstractModule { @Override protected void configure() { } @Provides @RequestScoped PersistenceManager providePersistenceManager(HttpServletRequest request) { return (PersistenceManager) request.getAttribute("pm"); } } </code></pre> <p>Finally you need to configure <a href="http://code.google.com/p/google-guice/wiki/GoogleAppEngine" rel="nofollow noreferrer">Google App Engine to use Guice</a>.</p> <p>This is just one possible way to do it. You could be more clever had have <code>providePersistenceManager</code> create the <code>PersistenceManager</code> and store it as a request attribute instead of having the servlet filter create the <code>PersistenceManager</code> (the filter would still close it). You could also have the servlet filter get the Guice Injector so you can avoid using <code>setAttribute()</code> and <code>getAttribute()</code> and instead use a more type-safe API.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload