【发布时间】: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