[英]Embedded JDO Field is not Retrieved by Query
我正在使用App Engine的JDO实现的本地开发版本。 当我查询包含其他对象作为嵌入式字段的对象时,嵌入式字段将返回为null。
例如,可以说这是我坚持的主要对象:
@PersistenceCapable
public class Branch {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long id;
@Persistent
private String name;
@Persistent
private Address address;
...
}
这是我的嵌入式对象:
@PersistenceCapable(embeddedOnly="true")
public class Address {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long id;
@Persistent
private String street;
@Persistent
private String city;
...
}
以下代码不检索嵌入式对象:
PersistenceManager pm = MyPersistenceManagerFactory.get().getPersistenceManager();
Branch branch = null;
try {
branch = pm.getObjectById(Branch.class, branchId);
}
catch (JDOObjectNotFoundException onfe) {
// not found
}
catch (Exception e) {
// failed
}
finally {
pm.close();
}
有人对此有解决方案吗? 如何检索分支对象及其嵌入式“地址”字段?
我有一个类似的问题,发现默认的访存组中未包含嵌入式字段。 要加载必需的字段,您要么必须在关闭持久性管理器之前为其调用getter,要么将fetch组设置为加载所有字段。
这意味着以下...
branch = pm.getObjectById(Branch.class, branchId);
pm.close();
branch.getAddress(); // this is null
branch = pm.getObjectById(Branch.class, branchId);
branch.getAddress(); // this is not null
pm.close();
branch.getAddress(); // neither is this
因此,您需要按以下方式更改代码:
Branch branch = null;
try {
branch = pm.getObjectById(Branch.class, branchId);
branch.getAddress();
}
catch (JDOObjectNotFoundException onfe) {
// not found
}
catch (Exception e) {
// failed
}
finally {
pm.close();
}
或者,您可以将获取组设置为包括所有字段,如下所示...
pm.getFetchPlan().setGroup(FetchGroup.ALL);
branch = pm.getObjectById(Branch.class, branchId);
pm.close();
branch.getAddress(); // this is not null
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.