I haven't tested this, but I believe this should work, yes. Let me know
if/when you try it.
As an aside, the object you return here won't actually be detached since
you're not using transactions (and hence, not committing). You'll want to
use something like the following or use a transaction.

object = pm.getObjectById(type,id);
object = pm.detachCopy(object);
...
return type.cast(object);

- Jason

On Tue, Sep 1, 2009 at 6:12 AM, funkforce <funkfo...@gmail.com> wrote:

>
> Hi,
>
> Regarding generic methods for presistance, has anybody been doing
> that?
> Like this for example:
>
> public static <T> T readDataObject(Class<T> type, Long id) {
>                Object object = null;
>                PersistenceManager pm = PMF.get().getPersistenceManager();
>                pm.setDetachAllOnCommit(true);
>        try {
>                object = pm.getObjectById(type,id);
>        } finally {
>            pm.close();
>        }
>        return type.cast(object);
> }
>
> You call it like this:
>
> Beverage b =  PersistenceHandler.readDataObject(Beverage.class,
> beverageId);
>
> Will this kind of code work?
>
> Thanks.
> FF
>
> On Sep 1, 11:54 am, funkforce <funkfo...@gmail.com> wrote:
> > Hi,
> >
> > First of all, thanks for posting, it really helps me.
> >
> > objectuser  : Good post and the link was really nice.
> > Albert Attard  : Thanks for the examples, it shows that you have to
> > wright lots of code to manage your own relationships (which I was
> > trying to avoid).
> > leszek: This is what I did and I was trying to avoid to handle
> > persistence issues as much as I could.
> >
> > I will re-model and start over and see how I should go on.
> >
> > Thanks.
> >
> > FF
> >
> > On Sep 1, 9:52 am, leszek <leszek.ptokar...@gmail.com> wrote:
> >
> >
> >
> > > You can follow something like that
> >
> > > class Parent {
> > >   ....
> > >   @Persistent
> > >   private List<Long/Key> childList;
> >
> > >   // non Persistent
> > >   private List<Child> list;
> > >   ...
> >
> > > }
> >
> > > class Child {
> >
> > >    @PrimaryKey
> > >    private Long/Key key;
> > >    ...
> >
> > > }
> >
> > > // before save Parent
> > >   private void beforeSave(EntityManager em,Parent pa) {
> > >      if (pa.getList() != null) {
> > >        List<Long> chList = new ArrayList<Long>();
> > >        for (Child ch : pa.getList) {
> > >          pm.makePersistent(ch);
> > >          pm.flush();
> > >          chList.add(ch.getKey());
> > >        }
> > >        pa.setChildList(chList);
> > >     }
> > >   }
> >
> > >   private void afterLoad(EntityManager em,Parent pa)  {
> > >     List<Child> chList = new ArrayList<Child>();
> > >     for (Long key : pa.getChildList()) {
> > >        Child ch = pm.getObjectById(Child.class,key);
> > >        chList.add(ch);
> > >     }
> > >     pa.setList(chList);
> > >   }
> >
> > >   PersistenManager pm;
> > >   ....
> >
> > >   // main service
> > >   Parent pa = pm..getObjectById(...)
> > >   afterLoad(pm,pa);
> > >   ...
> > >   ...
> > >   beforeSave(pm,pa);
> > >   pm.makePersistent(pa);
> >
> > >   List<Parent> pList = pm.queryExecute();
> > >   for (Parent pa : pList) {
> > >     afterLoad(pm,pa);
> > >   }
> > >   ...
> > >   // etc
> >
> > > In order to avoid repeating the same beforeSave and afterLoad for all
> > > Entity classes having "onowned" relationship you can apply Annotation
> > > and, by  the use of reflection, develop generic approach for all
> > > classes having this feature.- Hide quoted text -
> >
> > - Show quoted text -
> >
>

--~--~---------~--~----~------------~-------~--~----~
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-java@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