My experience with JPA in general has always been that operations were
wrapped in a transaction and changes were persisted on the commit() call of
the transaction object, not on the closing of the
PersistenceManager/EntityManager. When a transaction object is not used, App
Engine docs state this:

Referring to makePersistent:

The call to makePersistent() is synchronous, and doesn't return until the
object is saved and indexes are updated.

It's when you update an object that close() is used to flush the object to
persistent store:

One way to update an object with JDO is to fetch the object, then modify it
while the PersistenceManager that returned the object is still open. Changes
are persisted when the PersistenceManager is closed.

This is from this URL:
http://code.google.com/appengine/docs/java/datastore/creatinggettinganddeletingdata.html

On Tue, Dec 29, 2009 at 6:47 AM, ailinykh <[email protected]> wrote:

> No, not at all. makePersistent() means the object is managed by
> persistent manager. All changes made to this object should be stored.
> The "save" call is pm.close().
> And it works this way for fields like String.
> If I have class
> class Dummy{
>  @Persistent
>  String name;
>  @Persistent
>  List<Long> list;
> }
>
> and run this code
> Dummy d = new Dummy();
> d.setName("name");
> pm.makePersistent(d);
> d.setName("new name");
> pm.close();
>
> name "new name" will be stored. What is correct.
> But any modifications to list made after pm.makePersistent(d); are
> lost.
> This is a problem.
>
>
>
> On Dec 28, 5:30 pm, "Ikai L (Google)" <[email protected]> wrote:
> > This actually sounds like it is working correctly. makePersistent() is
> the
> > "save" call. You should not be calling this until all the changes you
> intend
> > on making have been made to the object you are persisting.
> >
> > Another option you have is to detach the object, but this has its own
> > gotchas as well.
> >
> >
> >
> > On Sat, Dec 26, 2009 at 11:55 AM, ailinykh <[email protected]> wrote:
> > > Ok. I narrowed down the problem.
> > > What I want is to maintain unowned relationship.
> > > I have simple class with set of Long (they are supposed to be ids of
> > > other objects):
> >
> > > @PersistenceCapable(identityType = IdentityType.APPLICATION)
> > > public class Dummy {
> > >        @Override
> > >        public String toString() {
> > >                StringBuilder sb = new StringBuilder();
> > >                sb.append(id);
> > >                sb.append(", Keys ");
> > >                sb.append(idSet);
> > >                return sb.toString();
> > >         }
> > >        @PrimaryKey
> > >    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
> > >    private Key id;
> > >        @Persistent
> > >         Set<Long> idSet = new HashSet<Long>();
> > >        public void addId(Long id){
> > >                idSet.add(id);
> > >        }
> > >        public Key getKey(){
> > >                return id;
> > >        }
> > > }
> >
> > > The problem is- if I create new object of class Dummy and make it
> > > persistent
> > > @Test
> > >        public void testIt(){
> > >                Dummy p = new Dummy();
> >
> > >                PersistenceManager pm =
> PMF.get().getPersistenceManager();
> > >                p.addId((long)1);
> > >                pm.makePersistent(p);
> > >                p.addId((long)2);
> > >                Key id = p.getKey();
> > >                pm.close();
> > >                pm = PMF.get().getPersistenceManager();
> > >                Dummy pp = pm.getObjectById(Dummy.class, id);
> > >                System.out.println(pp);
> > > }
> >
> > > it ignores all changes to idSet made after makePersistent() call. So,
> > > for code above I have:
> > > Dummy(1), Keys [1]
> > > Key 2 is lost.
> >
> > > But when I restore by
> > > pm.getObjectById(Dummy.class, id);
> > > everything looks good. I can add and more ids and restore them
> > > successfully:
> >
> > > @Test
> > >        public void testIt(){
> > >                Dummy p = new Dummy();
> >
> > >                PersistenceManager pm =
> PMF.get().getPersistenceManager();
> > >                p.addId((long)1);
> > >                pm.makePersistent(p);
> > >                p.addId((long)2);
> > >                Key id = p.getKey();
> > >                pm.close();
> > >                pm = PMF.get().getPersistenceManager();
> > >                Dummy pp = pm.getObjectById(Dummy.class, id);
> > >                System.out.println(pp);
> >
> > >                pp.addId((long)3);
> > >                pp.addId((long)4);
> > >                System.out.println(pp);
> > >                pm.close();
> > >                pm = PMF.get().getPersistenceManager();
> > >                Dummy ppp = pm.getObjectById(Dummy.class, id);
> > >                System.out.print(ppp);
> > >        }
> >
> > > here I have what I'm supposed to have:
> >
> > > Dummy(1), Keys [1]
> > > Dummy(1), Keys [1, 3, 4]
> > > Dec 26, 2009 1:43:29 PM org.datanucleus.ObjectManagerImpl close
> > > INFO: Outstanding nontx update being committed to datastore
> > > Dummy(1), Keys [1, 3, 4]
> >
> > > So, it looks like it is not possible to maintain unowned relationship.
> > > Did anybody create unowned relationship?
> >
> > > Thank you,
> > >  Andrey
> >
> > > On Dec 26, 12:10 am, ailinykh <[email protected]> wrote:
> > > > Hello, everybody!
> > > > I try to achieve simple thing - storing a set of keys.
> > > > I have two classes:
> >
> > > > @PersistenceCapable(identityType = IdentityType.APPLICATION)
> > > > public class Account {
> >
> > > >     @PrimaryKey
> > > >     @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
> > > >     private Key id;
> >
> > > >    @Persistent(mappedBy="account")
> > > >     List<Label> labels = new ArrayList<Label>();
> >
> > > >     @Persistent
> > > >     Set<Key> ids = new HashSet<Key>();
> >
> > > > }
> >
> > > > @PersistenceCapable(identityType = IdentityType.APPLICATION)
> > > > public class Label {
> > > >     @PrimaryKey
> > > >     @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
> > > >     private Key id;
> >
> > > >      @Persistent
> > > >      private Account account;
> >
> > > > }
> >
> > > > I created new Account
> >
> > > > Account a = new Account();
> > > > then added two Labels to Account, then added Label's keys to ids set.
> > > > As result I have for ids:
> > > > [Account(1)/Label(4), Account(1)/Label(3)]
> >
> > > > which looks correct.
> >
> > > > Then I called
> > > > pm.makePersistent(a),
> > > > pm.close();
> >
> > > > I created new instance of PersistenceManager and restore Account
> > > > object.
> > > > I get back two Labels, but their keys (ids set) is empty.
> > > > What did I do wrong?
> >
> > > > Thank you,
> > > >   Andrey
> >
> > > --
> >
> > > You received this message because you are subscribed to the Google
> Groups
> > > "Google App Engine" group.
> > > To post to this group, send email to [email protected]
> .
> > > To unsubscribe from this group, send email to
> > > [email protected]<google-appengine%[email protected]>
> <google-appengine%[email protected]<google-appengine%[email protected]>
> >
> > > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/google-appengine?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" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<google-appengine%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-appengine?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" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en.


Reply via email to