2

我尝试更新现有实体,但是在使用时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.fromAPIproperty.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);
}
4

1 回答 1

3

阅读本文档参考部分的“其他注意事项”:

https://code.google.com/p/objectify-appengine/wiki/Entities

Refs 不持有实体值;他们只需持有密钥,让您直接从会话中加载值(或数据存储,如果它不在会话中)。如果你想持久化一个新版本的地址,你需要明确地 save() 它。

另外:因为 Refs 不包含实体对象,所以不能有级联的 save() Refs!它们只是“指针”。

于 2013-08-04T16:34:02.177 回答