在客户端使用Ref.create()会导致错误

时间:2014-10-05 18:52:21

标签: java google-app-engine gwt objectify

我正在使用Objectify 5.0.5,Objectify-GWT 1.2,GWT 2.6.1和App Engine SDK 1.9.10。

我正在尝试用GAE,GWT和Objectify做一些非常基本的东西。我之前在GAE做过一些事情,现在我觉得我应该看看是否可以通过使用Objectify加快生产速度,但是我在障碍后爬上障碍。我不确定我做错了什么,或者库中是否有错误。

所以这是我的问题:我正在尝试在GWT中的客户端使用Ref.create(someEntity),但它失败并打印:

Invalid type signature for com.googlecode.objectify.impl.ref.DeadRef

我有以下实体/模型:

@Entity
public class Persistable implements Serializable{
    private static final long serialVersionUID = 7733004846243513450L;

    @Id
    Long id;

    public Persistable() {}

    public void setId(Long id) {
        this.id = id;
    }
    public Long getId()
    {
        return id;
    }

    public void persist()
    {
        //This is actually a "remote service" and the only thing it does is call:
       //ofy().save().entity(entity);
        PPServices.getBasicService().persist(this, new AsyncCallback<Void>() {
           @Override
           public void onFailure(Throwable caught) {}
           @Override
           public void onSuccess(Void result) {}
        });
    }
}


@Subclass
public class Event extends Persistable{

    private static final long serialVersionUID = 1L;

    Ref<PPUser> host = null;

    public Event(){}


    public void setHost(PPUser participant)
    {
        host = Ref.create(participant);
    }
}

@Subclass
public class PPUser extends Persistable{

    private static final long serialVersionUID = 1L;

    public PPUser(){}

}

我用它们如下:

public class EntryStuff implements EntryPoint{

    public void onModuleLoad() {

        final Event event = new Event();
        final PPUser user1 = new PPUser();
        event.setHost(user1);

        event.persist();
    }

我看不出有什么问题。即使我手动给user1一个ID,它也不会有任何区别。我是否被迫在服务器端设置主机字段或者什么?在JDO中,我只是创建一个新的PPUser实例并分配它然后保存/保存实体(在本例中为Event)并且它(PPUser)将被创建,这里可能不一样吗? :/

2 个答案:

答案 0 :(得分:1)

更新:尝试使用objectify-gwt-1.2.1;您仍然无法使用Ref.create(POJO)客户端,但Ref<?>应正确传输GWT-RPC。


Objectify提供的数据存储类的GWT仿真正在慢慢衰减。

嗨,我是Objectify的主要作者。我以前经常使用GWT,所以我积极维护objectify-gwt代码。但是,自从我上次使用GWT以来已经过了多年。在Objectify改变的同时,GWT发生了变化,甚至构建系统也发生了变化(objectify-gwt被拆分成一个单独的jar,因为旧方法给Gradle用户带来了问题)。

在某些特定情况下某些类型(特别是Ref和Key)的仿真中出现了某些问题。我不太了解GWT序列化的内部工作方式,足以解决它。

Objectify-gwt需要有固定利益的人帮助确保其有效。缺席,也许避免尝试使用Ref客户端?

答案 1 :(得分:0)

这个“黑客”解决了我的问题,我不会将此标记为答案,因为这不是一个可行的解决方案:

将以下方法添加到Persistable.java:

public Key<T> getKey() //Persistable is now actually Persistable<T>
{
    return Key.create(KeyFactory.createKey(getClass().getSimpleName(), id));
}

在Event.java中:

public void setHost(PPUser host) {
    this.host = Ref.create(host.getKey());
}

智能解决方案,因为文档声明我应该能够传递整个POJO实例。换句话说,不应该调用getKey(),而应该只能传递对象'host',它应该可以工作。这将在客户端工作,但它不会在数据存储上创建PPUser,因为它无法在没有实际ID的情况下在客户端创建密钥(意味着,如果id为null,则会出现异常)。

相关问题