![](/img/trans.png)
[英]Why doesn't my owned one-to-many relationship get persisted to the GAE datastore?
[英]difference of owned one-to-many relationship between GAE/J low level datastore API and DataNucleus JDO
在Java中创建拥有的一对多关系时,我注意到使用低级Datastore API和DataNucleus JDO之间的结果记录有所不同。 不知道这是有意的还是以任何方式解决它。
例如,
以下链接中是否有一个员工的多个地址:
https://developers.google.com/appengine/docs/java/datastore/entities#Ancestor_Paths
使用以下低级数据存储区api,员工记录不会显示地址列(即属性):
Entity employee = new Entity("Employee");
datastore.put(employee);
Entity address_home = new Entity("Address", employee.getKey());
datastore.put(address_home);
Entity address_mailing = new Entity("Address", employee.getKey());
datastore.put(address_mailing);
使用JDO,员工记录显示一个地址列(即属性):
@PersistenceCapable
public class Employee {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent(mappedBy = "employee")
private List<Address> addresses;
List<Address> getAddresses() {
return addresses;
}
void setAddresses(List<Address> addresses) {
this.addresses = addresses;
}
// ...
}
@PersistenceCapable
public class Address {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent
private Employee employee;
@Persistent
private String Street;
...
}
额外的财产是无害的。 但是,为什么JDO需要此功能?
我在开发服务器上使用带有DataNucleus v2的GAE / J 1.7.2。
GAE JDO插件的最新存储版本会将所有关系存储为一个属性,因此Employee类将为其存储的地址提供一个属性。 与GAE JDO过去用来存储事物的方式相比,这是一种更为合理的存储方式(它最初尝试使用另一方的所有权来模拟外键)。 将列表存储在所有者中具有将元素加载到集合中的优点,并且允许元素在列表中多次出现(而使用较旧的存储版本是不可能的)。
包括2.1.1在内的所有版本的GAE JDO都将索引位置存储在列表中的每个地址中,但实际上现在不需要存储它们了,因为Employee中的“ addresses”属性提供了-这是从早期开始遗留下来的需要以这种方式存储的版本。 从版本2.1.2开始,请勿将列表索引属性添加到元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.