我尝试更新现有实体,但是在使用时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);
}
有关更新实体的相关问题。以下调用是否同时更新 theRealEstateProperty
及其子项Address
?
public void update(RealEstateProperty property) {
ofy().save().entity(property);
}