This actually makes more sense. JPA does not write to the data store until
either the EntityManager is closed or the transaction is flushed. Here's an
example using transactions:

  EntityManager em = EMF.get().createEntityManager();
        PrintWriter out = response.getWriter();

        List<Dog> dogs;

        try {
            Dog newDog = new Dog("New dog!", 1);

            EntityTransaction tx = em.getTransaction();
            tx.begin();
            em.persist(newDog);
            tx.commit();

            Query query = em.createQuery("SELECT FROM Dog"); // WHERE key =
:key");
            dogs = (List<Dog>) query.getResultList();
       // etc

Here are some examples:
http://openjpa.apache.org/builds/1.0.2/apache-openjpa-1.0.2/docs/manual/jpa_overview_em_lifeexamples.html

On Mon, Nov 23, 2009 at 8:18 PM, lp <lucio.picc...@gmail.com> wrote:

>
> >         EntityManager em = EMF.get().createEntityManager();
> >         PrintWriter out = response.getWriter();
> >
> >         List<Dog> dogs;
> >
> >         try {
> >
> >             Query query = em.createQuery("SELECT FROM Dog WHERE
> > dogFriends = :key");
> >             query.setParameter("key", key);
> >             dogs = (List<Dog>) query.getResultList();
> >
> >             dogs.size();  // Do this so we can eager load the list and
>
> solve it!
>
> running your code in my unit test i still get a 0 size result list.
>
> *until* i added the following in my unittest
>
> Dog user1 = new Dog()
>
> //blah blah
> em.merge(user1);
>
> em.close();  <-- needed to close existing em
>
> em = getEnityManager();
>
> //query now executes correctly
>
>
> it seems that the entity manager is not working as i expected.
> by doing em.close after making objects persistent and then getting new
> em, allowed the em.query to execute correctly.
>
> i am not clear of why the em behaviours like that, given the em.find()
> seem to find the correct object.
>
> any ideas?
>
> -lp
>
> --
>
> You received this message because you are subscribed to the Google Groups
> "Google App Engine for Java" group.
> To post to this group, send email to
> google-appengine-j...@googlegroups.com.
> To unsubscribe from this group, send email to
> google-appengine-java+unsubscr...@googlegroups.com<google-appengine-java%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-appengine-java?hl=en.
>
>
>


-- 
Ikai Lan
Developer Programs Engineer, Google App Engine

--

You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to google-appengine-j...@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.


Reply via email to