0

我试图建立一个非常简单的 RequestFactory 示例,但我失败了。我将实体持久保存到我的数据存储中只是找到,但是当尝试再次将它们拉出时,我得到一个

com.google.web.bindery.requestfactory.server.UnexpectedException: The persisted entity with id aglub19hcHBfaWRyCgsSBFVzZXIYBAw has a null version

所以首先这是我的 JPO 注释实体类。最后你会找到一个静态函数供 RequestFactory 调用,以及一个非静态成员函数,它将成为一个 InstanceRequest。

package com.test.server;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.Column;
import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.jdo.annotations.Version;
import javax.jdo.annotations.VersionStrategy;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
@Version(strategy = VersionStrategy.VERSION_NUMBER, column = "VERSION", extensions = {         @Extension(vendorName = "datanucleus", key = "field-name", value = "version") })
public class User {

public User() {

}

public User(String name) {
    this.name = name;
}

@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
@Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
private String id;

@Persistent
@Column(name = "version")
private Integer version;      

@Persistent
private String name;

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public Integer getVersion() {
    return version;
}

public void setVersion(Integer version) {
    this.version = version;
}

public String getName() {
    return name;
}

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

  public static final PersistenceManager persistenceManager() {
        return PMF.get().getPersistenceManager();
      }

  @SuppressWarnings("unchecked")
  public static List<User> findAllUsers() {
      PersistenceManager pm = persistenceManager();
      try {
          String query = "SELECT FROM " + User.class.getName();
          List<User> objects = (List<User>) pm.newQuery(query).execute();
          objects.size(); // This is the workaround to retrieve all objects
          return objects;
      } finally {
          pm.close();
      }
  }

  public static User findUser(String id) {
      PersistenceManager pm = persistenceManager();
      try {
            User u = pm.getObjectById(User.class, id);
            return u;
      } finally {
          pm.close();
      }
  }

  public void persist() {
        PersistenceManager pm = persistenceManager();
        try {
          pm.makePersistent(this);
        } finally {
          pm.close();
        }
      }
  }

RequestFactory 接口本身真的很简单

 package com.test.shared;

 import com.google.web.bindery.requestfactory.shared.RequestFactory;

 public interface UserOrderRequestFactory extends RequestFactory {
UserRequest userRequest();
 }

对应的RequestContext也是如此

package com.test.shared;

import java.util.List;

import com.google.web.bindery.requestfactory.shared.InstanceRequest;
import com.google.web.bindery.requestfactory.shared.RequestContext;
import com.google.web.bindery.requestfactory.shared.Service;
import com.google.web.bindery.requestfactory.shared.Request;
import com.test.server.User;

@Service(User.class)
public interface UserRequest extends RequestContext {
    Request<List<UserProxy>> findAllUsers();
    InstanceRequest<UserProxy, Void> persist();
}

这是客户端的用户代理

package com.test.shared;

import com.google.web.bindery.requestfactory.shared.EntityProxy;
import com.google.web.bindery.requestfactory.shared.EntityProxyId;
import com.google.web.bindery.requestfactory.shared.ProxyFor;

@ProxyFor(com.test.server.User.class)
public interface UserProxy extends EntityProxy {
    EntityProxyId<UserProxy> stableId();
    String getName();
    void setName(String name);
}

最后是我的 onModuleLoad() ,它首先保留一个用户,然后获取所有用户的列表。

public void onModuleLoad() {

    final EventBus eventBus = new SimpleEventBus();
    requestFactory = GWT.create(UserOrderRequestFactory.class);
    requestFactory.initialize(eventBus);

    UserRequest userRequest = requestFactory.userRequest();

    UserProxy user = userRequest.create(UserProxy.class);
    user.setName("Luigi");

    userRequest.persist().using(user).fire( new Receiver<Void>()
            {
              @Override
                public void onSuccess(Void arg0)
                {
                    GWT.log("User persisted.");
                }
            });


    userRequest = requestFactory.userRequest();
    Request<List<UserProxy>> findAllUsersRequest = userRequest.findAllUsers();
    findAllUsersRequest.fire( new Receiver<List<UserProxy>>() {
        @Override
        public void onSuccess(List<UserProxy> list) {
            for(UserProxy u: list) {
                GWT.log(u.getName());
            }
        }
    });

欢迎任何意见。我很乐意收到这方面的任何建议。

先感谢您。

4

2 回答 2

0

我不确定这是否重要,但 @Version 列 =“VERSION”与 @Column(name = “version”) 不匹配。

然而,GAE 并没有真正的“列”,您可以通过删除列 = "" 和 @Column 来忽略它们。

http://gae-java-persistence.blogspot.com.au/2009/10/optimistic-locking-with-version.html

于 2013-11-23T06:32:39.487 回答
0

虽然 JPA 似乎自动执行此操作,但在 JDO 中推进版本计数器似乎是我的工作。我将以下代码添加到 User.java 中的持久例程中

// JPA @Version does this automatically, but JDO @Version is not working like that. Not sure why.
if (version == null) {
 version = 0l;
}
version++;
于 2012-08-11T16:06:50.793 回答