【问题标题】:GAE Data nucleus not fetching child entitiesGAE 数据核不获取子实体
【发布时间】:2012-12-14 09:11:54
【问题描述】:

我正在使用 JDO 版本 2 数据核和 App Engine SDK 1.7.3。我在检索一对多关系中的子对象时遇到问题。

contents = (Collection<ContentType>) persistenceManager.newQuery(ContentType.class).execute();

此查询返回 ContentType 的所有实例,但 ContentType 还包含未从数据库中获取的子“内容”列表。

ContentType.java

public Collection<ContentType> getAllContents() {

        PersistenceManager persistenceManager = PersistenceFactory
                .getInstance().getPersistenceManager();
        Transaction transaction = persistenceManager.currentTransaction();
        Collection<ContentType> contents = null;
        try {
            transaction.begin();
            contents = (Collection<ContentType>) persistenceManager.newQuery(ContentType.class).execute();


            Logging.log("contents in getAllContents=== "+ contents);

            if(contents!=null)
                {
                Logging.log("contents size in getAllContents=== "+ contents.size());

                }

        for (ContentType contentTypeElement : contents) {
            Logging.log("inside main loop in getAllContents, contentTypeElement=== "+ contentTypeElement);
            if(contentTypeElement!=null)
            {
                Logging.log("inside main loop in getAllContents, contentTypeElement having list === "+ contentTypeElement.getContentList());

            }
            if(contentTypeElement.getContentList()!=null)
            {

            for (Content contentElement : contentTypeElement.getContentList()) {
                Logging.log("inside second loop in getAllContents");
                String contentPath = Constants.CONTENT_BASE_URL
                        + contentElement.getContentPath();
                contentElement.setContentPath(contentPath);
            }
            }
            Logging.log("At end of main loop iteration");
        }
        transaction.commit();
        } catch (Exception e) {
            transaction.rollback();
            e.printStackTrace();
            Logging.log("Exception retriving all content, detail in getAllContents: "
                    + e.toString());

        } finally {
            persistenceManager.close();
        }
        return contents;
    }

这是日志

2012-12-14 13:27:14.101
org.datanucleus.TransactionImpl internalPreCommit: >> calling preCommit on org.datanucleus.store.connection.ConnectionManagerImpl$2@873b9f
I 2012-12-14 13:27:14.102
org.datanucleus.TransactionImpl internalPreCommit: >> calling preCommit on org.datanucleus.ObjectManagerImpl@c6b8b0
I 2012-12-14 13:27:14.102
org.datanucleus.TransactionImpl preFlush: >> calling preFlush on org.datanucleus.store.connection.ConnectionManagerImpl$2@873b9f
I 2012-12-14 13:27:14.102
org.datanucleus.TransactionImpl preFlush: >> calling preFlush on org.datanucleus.ObjectManagerImpl@c6b8b0
I 2012-12-14 13:27:14.135
com.test.igreetings.util.Logging log: contents in getAllContents=== [com.test.igreetings.persistence.domain.ContentType@14fdb76, com.test.igreetings.persistence.domain.ContentType@1d15a18, com.test.igreetings.persistence.domain.ContentType@171f735, com.test.igreetings.persistence.domain.ContentType@1bf496b]
I 2012-12-14 13:27:14.135
com.test.igreetings.util.Logging log: contents size in getAllContents=== 4
I 2012-12-14 13:27:14.135
com.test.igreetings.util.Logging log: inside main loop in getAllContents, contentTypeElement=== com.test.igreetings.persistence.domain.ContentType@14fdb76
I 2012-12-14 13:27:14.135
com.test.igreetings.util.Logging log: inside main loop in getAllContents, contentTypeElement having list === null
I 2012-12-14 13:27:14.135
com.test.igreetings.util.Logging log: At end of main loop iteration
I 2012-12-14 13:27:14.135
com.test.igreetings.util.Logging log: inside main loop in getAllContents, contentTypeElement=== com.test.igreetings.persistence.domain.ContentType@1d15a18
I 2012-12-14 13:27:14.135
com.test.igreetings.util.Logging log: inside main loop in getAllContents, contentTypeElement having list === null
I 2012-12-14 13:27:14.135
com.test.igreetings.util.Logging log: At end of main loop iteration
I 2012-12-14 13:27:14.135
com.test.igreetings.util.Logging log: inside main loop in getAllContents, contentTypeElement=== com.test.igreetings.persistence.domain.ContentType@171f735
I 2012-12-14 13:27:14.135
com.test.igreetings.util.Logging log: inside main loop in getAllContents, contentTypeElement having list === null
I 2012-12-14 13:27:14.135
com.test.igreetings.util.Logging log: At end of main loop iteration
I 2012-12-14 13:27:14.135
com.test.igreetings.util.Logging log: inside main loop in getAllContents, contentTypeElement=== com.test.igreetings.persistence.domain.ContentType@1bf496b
I 2012-12-14 13:27:14.135
com.test.igreetings.util.Logging log: inside main loop in getAllContents, contentTypeElement having list === null
I 2012-12-14 13:27:14.135
com.test.igreetings.util.Logging log: At end of main loop iteration
I 2012-12-14 13:27:14.136
org.datanucleus.TransactionImpl internalPreCommit: >> calling preCommit on org.datanucleus.store.connection.ConnectionManagerImpl$2@66da2c
I 2012-12-14 13:27:14.136
org.datanucleus.TransactionImpl internalPreCommit: >> calling preCommit on org.datanucleus.ObjectManagerImpl@274608

数据核日志

1.  org.datanucleus.store.connection.ConnectionManagerImpl allocateConnection: Connection added to the pool : com.google.appengine.datanucleus.DatastoreConnectionFactoryImpl$DatastoreManagedConnection@74ece8 for key=org.datanucleus.ObjectManagerImpl@107c76b in factory=ConnectionFactory:tx[com.google.appengine.datanucleus.DatastoreConnectionFactoryImpl@119549e]
2.  
3.  D2012-12-14 17:20:11.232
4.  org.datanucleus.store.query.QueryManager getQueryCompilationForQuery: Query "SELECT FROM com.test.igreetings.persistence.domain.ContentType" of language "JDOQL" has been run before so reusing existing generic compilation
5.  D2012-12-14 17:20:11.232
6.  com.google.appengine.datanucleus.query.JDOQLQuery performExecute: JDOQL Query : Executing "SELECT FROM com.test.igreetings.persistence.domain.ContentType" ...
7.  D2012-12-14 17:20:11.233
8.  com.google.appengine.datanucleus.query.JDOQLQuery performExecute: Query compiled as : Kind=com.test.igreetings.persistence.domain.ContentType
9.  D2012-12-14 17:20:11.233
10. com.google.appengine.datanucleus.query.DatastoreQuery executeNormalQuery: Executing query in datastore for SELECT FROM com.test.igreetings.persistence.domain.ContentType
11. D2012-12-14 17:20:11.233
12. org.datanucleus.store.connection.ConnectionManagerImpl allocateConnection: Connection found in the pool : com.google.appengine.datanucleus.DatastoreConnectionFactoryImpl$DatastoreManagedConnection@74ece8 for key=org.datanucleus.ObjectManagerImpl@107c76b in factory=ConnectionFactory:tx[com.google.appengine.datanucleus.DatastoreConnectionFactoryImpl@119549e]
13. D2012-12-14 17:20:11.234
14. com.google.appengine.datanucleus.query.JDOQLQuery performExecute: JDOQL Query : Execution Time = 2 ms
15. D2012-12-14 17:20:11.279
16. org.datanucleus.ObjectManagerImpl getObjectFromLevel1Cache: Object with id "com.test.igreetings.persistence.domain.ContentType:ContentType(3)" not found in Level 1 cache [cache size = 0]
17. D2012-12-14 17:20:11.279
18. org.datanucleus.ObjectManagerImpl putObjectIntoLevel1Cache: Object "com.test.igreetings.persistence.domain.ContentType@165d118" (id="com.test.igreetings.persistence.domain.ContentType:ContentType(3)") added to Level 1 cache (loadedFlags="[NNYNN]")
19. D2012-12-14 17:20:11.279
20. org.datanucleus.state.JDOStateManager wrapSCOField: Object "com.test.igreetings.persistence.domain.ContentType@165d118" (id="com.test.igreetings.persistence.domain.ContentType:ContentType(3)") is having the value in field "endDate" replaced by a SCO wrapper
21. D2012-12-14 17:20:11.280
22. org.datanucleus.state.JDOStateManager wrapSCOField: Object "com.test.igreetings.persistence.domain.ContentType@165d118" (id="com.test.igreetings.persistence.domain.ContentType:ContentType(3)") is having the value in field "startDate" replaced by a SCO wrapper
23. D2012-12-14 17:20:11.280
24. org.datanucleus.ObjectManagerImpl enlistInTransaction: Object "com.test.igreetings.persistence.domain.ContentType@165d118" (id="com.google.appengine.api.datastore.Key:ContentType(3)") enlisted in transactional cache
25. D2012-12-14 17:20:11.280
26. org.datanucleus.ObjectManagerImpl getObjectFromLevel2Cache: Object with id="com.test.igreetings.persistence.domain.ContentType:ContentType(3)" taken from Level 2 cache (fields="[0, 1, 2, 3, 4]", version="") - represented as "com.test.igreetings.persistence.domain.ContentType@165d118"
27. D2012-12-14 17:20:11.280
org.datanucleus.store.connection.ConnectionManagerImpl allocateConnection: Connection found in the pool : com.google.appengine.datanucleus.DatastoreConnectionFactoryImpl$DatastoreManagedConnection@74ece8 for key=org.datanucleus.ObjectManagerImpl@107c76b in factory=ConnectionFactory:tx[com.google.appengine.datanucleus.DatastoreConnectionFactoryImpl@119549e]

尽管在管理控制台数据查看器中有许多与内容类型相关联的内容实例,但 contentList 被提取为空。我错过了什么?

【问题讨论】:

  • 不见了?将其余代码发布在您检查集合字段是否为空、事务相对于该字段的位置、检查时 ContentType 对象所处的生命周期状态以及日志显示的内容
  • @DataNucleus。问题已编辑。添加了代码和日志
  • 日志的其余部分在哪里?数据核日志。您调用查询的位置,它会告诉您它在做什么
  • 集合字段属性的数据存储区中有什么? ContentType 类的其余部分是什么?您可以轻松地与 code.google.com/p/datanucleus-appengine/source/browse/… 下的所有具有集合的测试进行比较并毫无问题地加载它们
  • 在数据存储中,当我单击子记录的主键时,我得到的是:解码的实体键:ContentType:id=3 > Content:id=8 这意味着 ContentType 对象( Id 3 的 parent) 在其主键上与此内容(子)记录相关联。我们如何告诉 java 实体整个对象与您的主键相关联?

标签: java google-app-engine jdo datanucleus


【解决方案1】:

既然您现在说 List 的 ContentType 中没有属性,那么您没有使用 GAE JDO 插件的 v2 保存数据。因此,您不能指望它知道集合中的元素。

解决方案:修复您的数据。

【讨论】:

  • 谢谢。我相信它会起作用。是否有任何链接可以帮助我更好地理解这个问题。
  • 我能找到的唯一链接是code.google.com/p/datanucleus-appengine/wiki/…,它描述了一种迁移 GAE JDO v1 数据的方法。或者,您可以手动执行此操作,方法是向所有者对象添加属性并将 List 与元素的键一起存储。或者,只保留一个新的所有者+元素,并查看它作为指南创建的内容
  • 虽然有一个小问题。当我直接通过控制台更新/删除数据存储中的数据时,我的 java 实体没有更新,并且我一直处于旧状态。
  • 如果您通过 JDO 以外的其他机制删除集合中的对象,那么您有责任更新所有者对象中的 List 字段。
  • List 是持久的并存在于我的实体中,但我在数据存储区中看不到任何反对它的东西(尽管此列表在 java 实体中被正确获取)。有没有办法将我的 java 实体与数据存储的最新状态同步?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-25
相关资源
最近更新 更多