0

使用 Objectify 4,当我保存 MenuItem 对象时,名称和价格字段不会保留。只有 Key、WriteOps 和 ID/Name 保留在调试数据存储区中。

@Entity
public class MenuItem extends BaseEntity {
private String name;
private double price;

public MenuItem() {
}

public MenuItem(String name, double price) {
    this.name = name;
    this.price = price;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public double getPrice() {
    return price;
}

public void setPrice(double price) {
    this.price = price;
}
}

BaseEntity 在哪里:

public abstract class BaseEntity implements Dto {
private static final long serialVersionUID = 8400346403708831769L;

@Id
protected Long id;

protected BaseEntity() {
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((id == null) ? 0 : id.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }

    if (obj == null) {
        return false;
    }

    if (getClass() != obj.getClass()) {
        return false;
    }

    BaseEntity other = (BaseEntity) obj;

    if (id == null) {
        if (other.id != null) {
            return false;
        }
    } else if (!id.equals(other.id)) {
        return false;
    }

    return true;
}

public Long getId() {
    return id;
}

public void setId(@Nullable Long id) {
    this.id = id;
}

public boolean isSaved() {
    return (id != null);
}
}

Dto 是:

public interface Dto extends Serializable {
}

有任何想法吗?谢谢!

4

1 回答 1

1

如 Objectify 邮件列表中所述,可能只是您查看的数据存储区缺少此信息。

您能否显示一些测试代码来保存这些数据,然后检索它并打印出结果,而不是使用数据存储查看器?我希望:

MenuItem toSave = new MenuItem("test", 6.3);
toSave.setId(1);
ofy().save().entity(toSave).now();
ofy().clear(); //Make sure we're not just looking at the current session.
MenuItem loaded = ofy().load().type(MenuItem.class).id(1).get();
assertEquals(loaded.getName(), "test");
assertEquals(loaded.getPrice(), 6.3);
于 2012-08-17T07:56:03.377 回答