升级到2.0版最终版本的datanucleus-appengine后,在通过查询API使用JDOQL查询时,现在似乎忽略了获取组。我不确定它是否是库中的错误或我做错了。
我定义了一个'User'实体,其中包含对其他实体的多个1:M List引用。我们通过提取组来控制这些引用的获取,例如:
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
@FetchGroups({@FetchGroup(name="bits",
members=@Persistent(name="userHistory"),
@Persistent(name="userBranding")}),
@FetchGroup(name="content", members={@Persistent(name="userContent")})})
public class User implements Serializable {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private String username;
@Persistent
private String email;
@Persistent(mappedBy = "contentUser", defaultFetchGroup = "false")
@Element(dependent = "true")
@Order(extensions = @Extension(vendorName = "datanucleus", key = "list-ordering",value="contentKey asc"))
private List<Content> userContent;
@Persistent(mappedBy = "historyUser", defaultFetchGroup = "false")
@Element(dependent = "true")
@Order(extensions = @Extension(vendorName = "datanucleus", key = "list-ordering", value = "createdDate asc"))
private List<History> userHistory;
@Persistent(mappedBy = "brandingUser", defaultFetchGroup = "false")
@Element(dependent = "true")
private List<MediaBranding> userBranding;
...
}
根据上述定义,我的理解是:
使用JDOQL通过电子邮件字段获取用户时,无论指定哪个获取组,都不会获取任何关系。如果我使用getById()来获取用户,则会按预期应用提取组。
我已关闭L2缓存(在jdoquery.xml中设置为NONE)。
我用来获取的测试用例如下所示(使用版本2.0.0-final失败,通过版本1.0.10)。
@SuppressWarnings("unchecked")
@Test
public void testAddRetrieveUserByJDOQLQueryFetchGroups() {
PersistenceManager pm;
User u = persistUserAndFriends();
pm = getPMF().getPersistenceManager();
User result = null;
try {
pm.getFetchPlan().addGroup("bits");
pm.getFetchPlan().addGroup("content");
log.info("Groups in fetch plan: " + pm.getFetchPlan().getGroups());
Query query = pm.newQuery(User.class);
query.setFilter("email == emailParam");
query.declareParameters("java.lang.String emailParam");
List<User> results = (List<User>) query.execute("hello@site.net");
assertEquals(1, results.size());
result = results.get(0);
pm.makeTransient(result, true);
} finally {
pm.close();
}
assertNotNull("User history should be fetched", result.getUserHistory());
assertNotNull("User branding should be fetched", result.getUserBranding());
assertEquals("Should be two history children", 2, result.getUserHistory().size());
assertEquals("Should be two branding children", 2, result.getUserBranding().size());
assertEquals("Should be a content child", 1, result.getUserContent().size());
}
我是以错误的方式解决这个问题吗?