[英]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.