Question

I am in the process of moving my code to Java7 and to the newest Datanucleus App Engine Plugin and I am stuck with the order problem.

I have an ancestor class that owns some children that need to mantain a specific order defined by the user. So that I used the @Order annotion without any @Extension. With the previous JDO version everything worked fined and now my unit test fails because it doesn't find anymore this _INTEGER_IDX property.

I created a simple test project, following this documentation Owned one-to-many relationship in order to start from a fresh example and still it doesn't work: GitHub-project.

I summarize here the model:

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Book {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    @Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
    private String key = null;

    @Persistent
    private String title = null;

    @Persistent(mappedBy="book")
    @Order
    private List<Chapter> chapters = null;

.. and ..

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Chapter {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    @Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
    private String key = null;

    @Persistent
    private String name = null;

    @Persistent
    private Book book = null;

.. and here is the Unit Test that fails:

@Test
public void testJdoDependingEntities() throws EntityNotFoundException {
    PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory("transactions-optional");

    PersistenceManager pm = pmf.getPersistenceManager();

    Book bookJdo = null;

    try {
        pm.currentTransaction().begin();

        bookJdo = new Book("myTitle");
        Chapter cpt1 = new Chapter("myname1");
        Chapter cpt2 = new Chapter("myname2");
        Chapter cpt3 = new Chapter("myname3");
        Chapter cpt4 = new Chapter("myname4");
        Chapter cpt5 = new Chapter("myname5");
        Chapter cpt6 = new Chapter("myname6");

        bookJdo.getChapters().add(cpt1);
        cpt1.setBook(bookJdo);
        bookJdo.getChapters().add(cpt2);
        cpt2.setBook(bookJdo);
        bookJdo.getChapters().add(cpt3);
        cpt3.setBook(bookJdo);
        bookJdo.getChapters().add(cpt4);
        cpt4.setBook(bookJdo);
        bookJdo.getChapters().add(cpt5);
        cpt5.setBook(bookJdo);
        bookJdo.getChapters().add(cpt6);
        cpt6.setBook(bookJdo);

        pm.makePersistent(bookJdo);

        pm.currentTransaction().commit();
    } catch(Exception e) {
        e.printStackTrace();
    }
    finally {
        if(pm.currentTransaction().isActive()) {
            pm.currentTransaction().rollback();
        }
        pm.close();
    }

    Entity book = datastore.get(KeyFactory.stringToKey(bookJdo.getKey()));
    assertEquals("myTitle", book.getProperty("title"));

    Query query = new com.google.appengine.api.datastore.Query(
            "Chapter",
            KeyFactory.stringToKey(bookJdo.getKey()));
    FetchOptions options = FetchOptions.Builder.withDefaults();

    QueryResultList<Entity> resultEntity = datastore.prepare(query)
            .asQueryResultList(options);

    assertEquals(6, resultEntity.size());

    // print values
    System.out.println("From ancestor query:");
    System.out.println("All the props: "+resultEntity.get(0).getProperties());
    System.out.println("idx prop: "+resultEntity.get(0).getProperty("chapters_INTEGER_IDX"));
    System.out.println();

    Entity chapter = datastore.get(resultEntity.get(0).getKey());
    System.out.println("All the props: "+chapter.getProperties());
    System.out.println("idx prop: "+chapter.getProperty("chapters_INTEGER_IDX"));

    // test against JUnit
    assertNotNull(resultEntity.get(0).getProperty("chapters_INTEGER_IDX"));
    assertNotNull(chapter.getProperty("chapters_INTEGER_IDX"));
 }

Here is the GitHub-project.

Was it helpful?

Solution 2

Found the problem!

You should add this line in the jdoconfig.xml:

<property name="datanucleus.appengine.storageVersion" value="PARENTS_DO_NOT_REFER_TO_CHILDREN"/>

Here is the piece of code where it is explained the different datastore configurations

OTHER TIPS

Why does "Chapter.book" have a mappedBy on it? That is illegal. If a relation is bidirectional then it has a mappedBy at the side that doesn't have the FK.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top