daily pastebin goal
58%
SHARE
TWEET

Untitled

a guest Jul 18th, 2018 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import EMF;
  2. import Dog;
  3.  
  4. import com.google.appengine.api.datastore.Key;
  5. import com.google.appengine.api.datastore.KeyFactory;
  6.  
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import javax.servlet.ServletException;
  11. import javax.persistence.EntityManager;
  12. import javax.persistence.Query;
  13. import java.io.IOException;
  14. import java.io.PrintWriter;
  15. import java.util.List;
  16.  
  17. public class ListFriendsServlet extends HttpServlet {
  18.  
  19.     @SuppressWarnings("unchecked")
  20.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  21.         String keyString = request.getParameter("friend");
  22.         Key key = KeyFactory.stringToKey(keyString);
  23.  
  24.         EntityManager em = EMF.get().createEntityManager();
  25.         PrintWriter out = response.getWriter();
  26.  
  27.         List<Dog> dogs;
  28.  
  29.         try {
  30.  
  31.             Query query = em.createQuery("SELECT FROM Dog WHERE dogFriends = :key");
  32.             query.setParameter("key", key);
  33.             dogs = (List<Dog>) query.getResultList();
  34.  
  35.             dogs.size();  // Do this so we can eager load the list and close the EMF
  36.         } finally {
  37.             em.close();
  38.         }
  39.  
  40.         out.println("Listing dogs");
  41.         for(Dog dog : dogs) {
  42.             out.println(dog);
  43.         }
  44.     }
  45. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top