我遇到了孩子没有引用其父母的问题。据说我已经建立了双向关系,我可以从父母那里得到孩子并从孩子那里得到父母;但是我孩子的父母总是回来了。
我有一个父母,
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Company {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent(mappedBy = "company")
private List<Employee> employees = new ArrayList<Employee>();
}
带着孩子,
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Employee {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent
private Company company;
@Persistent
private String username;
}
我坚持这个对象,
Company newCompany = new Company();
Employee newEmployee = new Employee();
newEmployee.setUsername("ryan");
newCompany.getEmployees().add(newEmployee);
pm.makePersistent(newCompany);
我正在访问这样的对象,
Query query = pm.newQuery(Employee.class,"username == s");
query.declareParameters("String s");
List<Employee> employees = (List<Employee>)query.execute(username);
pm.close();
if (employees.size() > 0)
{
Employee employee = employeeList.get(0);
...
}
我在调试时看到“company”为null,而其余的员工字段都已填充。对我缺少什么的想法?
答案 0 :(得分:1)
根据对象状态,获取父级取决于您检查它的位置。如果您的PM已被该点关闭,那么它将不会获取父字段。在关闭PM之前触摸父字段(并将retainValues设置为true,或者在PM结束时分离)将导致父字段被设置。