使用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 {
}
有什么想法吗?谢谢!
答案 0 :(得分: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);