Cha*_*nia 5 java google-app-engine jdo datanucleus
我正在使用App Engine的JDO实现的本地开发版本.当我查询包含其他对象作为嵌入字段的对象时,嵌入字段将返回null.
例如,假设这是我坚持的主要对象:
@PersistenceCapable
public class Branch {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long id;
@Persistent
private String name;
@Persistent
private Address address;
...
}
Run Code Online (Sandbox Code Playgroud)
这是我的嵌入对象:
@PersistenceCapable(embeddedOnly="true")
public class Address {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long id;
@Persistent
private String street;
@Persistent
private String city;
...
}
Run Code Online (Sandbox Code Playgroud)
以下代码不检索嵌入对象:
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();
}
Run Code Online (Sandbox Code Playgroud)
有人有解决方案吗?如何检索Branch对象及其嵌入的Address字段?
我遇到了类似的问题,发现嵌入字段不包含在默认的提取组中.有需要的领域加载你将不得不关闭持久性管理或设置获取群组加载所有领域的时候去拜访它的吸气剂.
这意味着以下......
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
Run Code Online (Sandbox Code Playgroud)
因此,您需要更改代码,如下所示:
Branch branch = null;
try {
branch = pm.getObjectById(Branch.class, branchId);
branch.getAddress();
}
catch (JDOObjectNotFoundException onfe) {
// not found
}
catch (Exception e) {
// failed
}
finally {
pm.close();
}
Run Code Online (Sandbox Code Playgroud)
或者,您可以将获取组设置为包括以下所有字段...
pm.getFetchPlan().setGroup(FetchGroup.ALL);
branch = pm.getObjectById(Branch.class, branchId);
pm.close();
branch.getAddress(); // this is not null
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1122 次 |
| 最近记录: |