Dear group,

I would like to know the reason why child value won't be stored when
it's persisted directly by referring as a element of list field of
parent.
In the next testDetach7 JUnit test (which tries to change the value of
existing child entity field),
    - chapter child object was not stored by persisting like
pm.makePersistent( b.getChapters().get( 0))
    - chapter child object was stored by persisting the parent like by
pm.makePersistent( b);

        public void testDetach7() throws Exception {
                prepTestObjects();

                int originalNumPages = b.getChapters().get( 0).getNumPages();
                do {
                        b.getChapters().get( 0).setNumPages(
                                        new Double( Math.round( Math.random() * 
99)).intValue() + 1
                                        );
                } while( originalNumPages == b.getChapters().get( 
0).getNumPages());
                int newNumPages = b.getChapters().get( 0).getNumPages();

/*
                pm = pmf.getPersistenceManager();
                pm.makePersistent( b.getChapters().get( 0));
                Chapter detachedChapter =
(Chapter)pm.detachCopy( b.getChapters().get( 0));
                pm.close();
This fails
                assertEquals( newNumPages, detachedChapter.getNumPages());
*/

                pm = pmf.getPersistenceManager();
                pm.makePersistent( b);
                Chapter detachedChapter =
(Chapter)pm.detachCopy( b.getChapters().get( 0));
                pm.close();
// This will pass
                assertEquals( newNumPages, detachedChapter.getNumPages());

        }


        protected void prepTestObjects() throws Exception {
                b = new Book();
                b.setTitle( "JDO 4eva");

                c1 = new Chapter();
                c1.setTitle( "Intro");
                c1.setNumPages( 10);

                b.getChapters().add( c1);

                c2 = new Chapter();
                c2.setTitle( "Configuration");
                c2.setNumPages( 9);
                b.getChapters().add( c2);

                pm = pmf.getPersistenceManager();
                pm.setDetachAllOnCommit( true);
                try {
                        pm.currentTransaction().begin();
                    pm.makePersistent( b);
                    pm.currentTransaction().commit();
                } finally {
                    if (pm.currentTransaction().isActive()) {
                        pm.currentTransaction().rollback();
                    }
                    pm.close();
                }
        }

/**
 * From 
http://gae-java-persistence.blogspot.com/2009/10/creating-bidrectional-owned-one-to-many.html
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION,
detachable = "true")
public class Book {

        // Prefer String encodedKey over Key class object since it's sharable
with client via serialization
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
        @Extension( vendorName = "datanucleus", key = "gae.encoded-pk",
value="true")
        protected String encodedKey;

        protected String title;

    @Persistent(mappedBy = "book", defaultFetchGroup="true")
    @Element( dependent = "true")
    protected List<Chapter> chapters = new ArrayList<Chapter>();

    - getter and setters -
}

@PersistenceCapable( identityType = IdentityType.APPLICATION,
detachable = "true")
public class Chapter {
        // Prefer String encodedKey over Key class object since it's sharable
with client via serialization
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
        @Extension( vendorName = "datanucleus", key = "gae.encoded-pk",
value="true")
        protected String encodedKey;

    protected String title;
    protected int numPages;

    @Persistent( defaultFetchGroup = "true")
    protected Book book;

    - getter and setters -
}

-- 
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