我正在将我的代码迁移到 Java7 和最新的 Datanucleus App Engine 插件,但我遇到了顺序问题。
我有一个祖先类,它拥有一些需要维护用户定义的特定顺序的子类。所以我使用了没有任何@Extension 的@Order 注释。 使用以前的 JDO 版本一切正常,现在我的单元测试失败了,因为它不再找到这个 _INTEGER_IDX 属性。
我按照此文档创建了一个简单的测试项目 Owned one-to-many relationship为了从一个新的例子开始但它仍然不起作用: GitHub-project .
我在这里总结了这个模型:
@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;
..和..
@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;
.. 这是失败的单元测试:
@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"));
}
这是 GitHub-project .
最佳答案
为什么“Chapter.book”上有一个 mappedBy?那是非法的。如果关系是双向的,那么它在没有 FK 的一侧有一个 mappedBy。
关于java - 在 Google App Engine 中使用 JDO 定义的集合中保留任意顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22256141/