As a quick sanity check, can you issue an ancestor query on the stored
Source using the low-level datastore API? This will help verify whether all
News entities are being saved in the same entity group as the Source entity,
which is the implicit assumption here.
Also, you should modify your loadAll method to close the PersistenceManager
before exiting the method. As it stands, your application is leaking
resources every time this method is called.

- Jason

On Thu, Oct 15, 2009 at 3:08 AM, Shponter <[email protected]> wrote:

>
> Hi.
>
> I have a problem with saving parent entity (Source) when saving child
> entity (News) in one-to-many relationship.
> I try to save 5 News entities and only in first one the parent Source
> field is saved...
>
> Entities:
>
> @Entity
> public class Source {
>
>        @Id
>        @GeneratedValue(strategy = GenerationType.IDENTITY)
>        private Key id;
>
>        private String name;
>        ...
>
> @Entity
> public class News {
>
>        @Id
>        @GeneratedValue(strategy = GenerationType.IDENTITY)
>        private Key id;
>
>        private String title;
>
>        @ManyToOne
>        private Source source;
>        ...
>
>
>
> Test code:
>
> public void testSave() {
>        Source source = new Source("Source");
>        source = sourceDao.save(source);
>        assertEquals(1, ds.prepare(new Query(Source.class.getSimpleName
> ())).countEntities());
>        source = sourceDao.get(source.getId());
>        assertNotNull(source);
>        assertEquals("Source", source.getName());
>        for (int i = 0; i < 5; i++) {
>                News news = new News("Title-"+i);
>                news.setSource(source);
>                newsDao.save(news);
>        }
>        assertEquals(5, ds.prepare(new Query(News.class.getSimpleName
> ())).countEntities());
>        List<News> newsList = newsDao.loadAll();
>        for (News n : newsList) {
>                System.out.println("news: "+n.getTitle()+", s:
> "+n.getSource());
>        }
> }
>
>
> Result of this code:
>
> news: Title-0, s: sou...@aa233f
> news: Title-1, s: null
> news: Title-2, s: null
> news: Title-3, s: null
> news: Title-4, s: null
>
>
> My generic DAO code looks like this:
>
> public T get(PK id) {
>        PersistenceManager pm = PMF.get().getPersistenceManager();
>        try {
>                return (T) pm.getObjectById(this.type, id);
>        } finally {
>                pm.close();
>        }
> }
>
> public T save(T o) {
>        PersistenceManager pm = PMF.get().getPersistenceManager();
>        try {
>                return (T) pm.makePersistent(o);
>        } finally {
>                pm.close();
>        }
> }
>
> @SuppressWarnings("unchecked")
> public List<T> loadAll() {
>        PersistenceManager pm = PMF.get().getPersistenceManager();
>        return (List<T>) pm.newQuery(this.type).execute();
> }
>
>
> Thanks in advance for any help!
>
> Cheers!
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
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 [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-java?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to