我尝试更新现有实体,但在使用Ref.create
时,会使用缓存值。客户端对象是:
public class RealEstatePropertyAPI {
private Long id;
private String name;
private AddressAPI address;
}
public class AddressAPI {
private Long id;
private String street;
private String city;
private String state;
private String zip;
private String country;
}
服务器端的相应对象:
@Entity
public class RealEstateProperty {
@Id
private Long id;
@Parent
private Ref<Owner> owner;
private String name;
@Load
private Ref<Address> address;
}
@Entity
public class Address {
@Id
private Long id;
private String street;
private String city;
private String state;
private String zip;
private String country;
}
我有一种方法可以从客户端对象转换为服务器对象:
public static RealEstateProperty fromAPI(RealEstatePropertyAPI api,
Owner owner) {
RealEstateProperty property = new RealEstateProperty();
property.setId(api.getId());
property.setName(api.getName());
property.setOwner(owner);
if (api.getAddress() != null) {
Address address = AddressConverter.fromAPI(api.getAddress());
String x = address.getStreet();
property.setAddress(address);
String y = property.getAddress().getStreet();
System.out.println(x.equals(y));
}
return property;
}
public static Address fromAPI(AddressAPI api) {
Address address = new Address();
address.setId(api.getId());
address.setStreet(api.getStreet());
address.setCity(api.getCity());
address.setState(api.getState());
address.setZip(api.getZip());
address.setCountry(api.getCountry());
return address;
}
问题出在RealEstateProperty.fromAPI
,property.setAddress(address);
应该设置新的Address
,但事实并非如此。为什么x.equals(y)
返回false?
方法:
public void setAddress(Address value) {
this.address = Ref.create(value);
}
有关更新实体的相关问题。以下调用是否同时更新了RealEstateProperty
及其子Address
?
public void update(RealEstateProperty property) {
ofy().save().entity(property);
}
答案 0 :(得分:3)
阅读本文档参考部分的“其他注意事项”:
https://code.google.com/p/objectify-appengine/wiki/Entities
参考不持有实体价值;他们只需按住键,然后直接将值加载到会话中(如果不在会话中,则加载数据存储区)。如果要保留新版本的地址,则需要明确地保存()。
另外:Refs没有级联的save(),因为Refs不包含实体对象!它们只是“指针”。