使用 rpc,使用 jdo 将 gwt 实体保存到 google 应用程序引擎数据存储区

发布于 2024-10-07 22:40:17 字数 4487 浏览 0 评论 0原文

您好,我是 GWT 框架的新手。我想使用 rpc 将我的域对象/实体持久保存到 google 应用程序引擎数据存储区。一个简单的实现,用于测试我是否可以进行多个 rpc 调用(greetServer()、saveStudent()

Student

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.gwt.user.client.rpc.IsSerializable;

@PersistenceCapable
public class Student implements IsSerializable  {

private static final long serialVersionUID = 1L;

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

 @Persistent private String firstName;
 @Persistent private String lastName;

public Student(){}

 public Student(String firstName, String lastName){
  this.firstName = firstName;
  this.lastName  = lastName;
 }

 public void setStudentId(int studentId) {
  this.studentId = studentId;
 }

 public int getStudentId() {
  return studentId;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getFirstName() {
  return firstName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public String getLastName() {
  return lastName;
 }
}

GreetingService (由 Eclipse IDE 生成的默认代码)

import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;

@RemoteServiceRelativePath("greet")
public interface GreetingService extends RemoteService {
 String greetServer(String name) throws IllegalArgumentException;
 **String saveStudent(Student s) throws IllegalArgumentException;**
}

GreetingServiceAsync

import com.google.gwt.user.client.rpc.AsyncCallback;


public interface GreetingServiceAsync {
 void greetServer(String input, AsyncCallback<String> callback)
   throws IllegalArgumentException;
 **void saveStudent(Student s, AsyncCallback<String> callback)
   throws IllegalArgumentException;**
}

GreetingServiceImpl

import javax.jdo.PersistenceManager;

import com.d.client.GreetingService;
import com.d.client.Student;
import com.d.shared.FieldVerifier;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements
  GreetingService {

 public String greetServer(String input) throws IllegalArgumentException 

  ...

  String serverInfo = getServletContext().getServerInfo();
  String userAgent = getThreadLocalRequest().getHeader("User-Agent");

  ...

 }


 @Override
 public String saveStudent(Student s) throws IllegalArgumentException {
  PersistenceManager pm = PMF.get().getPersistenceManager();
  pm.makePersistent(s);
  return "student save - ok";
  }

 }

< strong>PMF

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManagerFactory;

public final class PMF {
    private static final PersistenceManagerFactory pmfInstance = JDOHelper
            .getPersistenceManagerFactory("transactions-optional");

    private PMF() {
    }

    public static PersistenceManagerFactory get() {
        return pmfInstance;
    }
}

EntryPoint

...

private final GreetingServiceAsync greetingService = GWT
    .create(GreetingService.class);

    greetingService.greetServer("greet",
      new AsyncCallback<String>() {
       public void onFailure(Throwable caught) {
        // Show the RPC error message to the user
       }

       public void onSuccess(String result) {
        //Show success message
       }
      });

    greetingService.saveStudent(new Student("kostas","trichas"),
      new AsyncCallback<String>() {
       public void onFailure(Throwable caught) {
        // Show the RPC error message to the user    
       }

       public void onSuccess(String result) {
        //Show success message
       }
      });

   ...

上述实现是否正确?我将此示例应用程序部署到 gae,但它没有保留对象 student(您可以在 gae 数据存储查看器中浏览实体)

请检查:

http://gwtgaedatastore.appspot.com

Hello iam new to GWT framework. I want to persist my domain objects/entities to google application engine datastore using rpc. A simple implementation to test if i can make multiple rpc calls ( greetServer() , saveStudent() )

Student

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.gwt.user.client.rpc.IsSerializable;

@PersistenceCapable
public class Student implements IsSerializable  {

private static final long serialVersionUID = 1L;

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

 @Persistent private String firstName;
 @Persistent private String lastName;

public Student(){}

 public Student(String firstName, String lastName){
  this.firstName = firstName;
  this.lastName  = lastName;
 }

 public void setStudentId(int studentId) {
  this.studentId = studentId;
 }

 public int getStudentId() {
  return studentId;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getFirstName() {
  return firstName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public String getLastName() {
  return lastName;
 }
}

GreetingService (default code generated by Eclipse IDE)

import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;

@RemoteServiceRelativePath("greet")
public interface GreetingService extends RemoteService {
 String greetServer(String name) throws IllegalArgumentException;
 **String saveStudent(Student s) throws IllegalArgumentException;**
}

GreetingServiceAsync

import com.google.gwt.user.client.rpc.AsyncCallback;


public interface GreetingServiceAsync {
 void greetServer(String input, AsyncCallback<String> callback)
   throws IllegalArgumentException;
 **void saveStudent(Student s, AsyncCallback<String> callback)
   throws IllegalArgumentException;**
}

GreetingServiceImpl

import javax.jdo.PersistenceManager;

import com.d.client.GreetingService;
import com.d.client.Student;
import com.d.shared.FieldVerifier;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements
  GreetingService {

 public String greetServer(String input) throws IllegalArgumentException 

  ...

  String serverInfo = getServletContext().getServerInfo();
  String userAgent = getThreadLocalRequest().getHeader("User-Agent");

  ...

 }


 @Override
 public String saveStudent(Student s) throws IllegalArgumentException {
  PersistenceManager pm = PMF.get().getPersistenceManager();
  pm.makePersistent(s);
  return "student save - ok";
  }

 }

PMF

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManagerFactory;

public final class PMF {
    private static final PersistenceManagerFactory pmfInstance = JDOHelper
            .getPersistenceManagerFactory("transactions-optional");

    private PMF() {
    }

    public static PersistenceManagerFactory get() {
        return pmfInstance;
    }
}

EntryPoint

...

private final GreetingServiceAsync greetingService = GWT
    .create(GreetingService.class);

    greetingService.greetServer("greet",
      new AsyncCallback<String>() {
       public void onFailure(Throwable caught) {
        // Show the RPC error message to the user
       }

       public void onSuccess(String result) {
        //Show success message
       }
      });

    greetingService.saveStudent(new Student("kostas","trichas"),
      new AsyncCallback<String>() {
       public void onFailure(Throwable caught) {
        // Show the RPC error message to the user    
       }

       public void onSuccess(String result) {
        //Show success message
       }
      });

   ...

Is the above implementation correct? I deployed this sample application to gae and it did not persisted the object student (you can browse the entities at gae datastore viewer)

check it please:

http://gwtgaedatastore.appspot.com

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帅气称霸 2024-10-14 22:40:17

将您的 int StudentID 更改为 Long id 以使其正常工作

Change your int studentID to Long id to get it working

猫九 2024-10-14 22:40:17

这适用于您的原始代码(即长 id):

@Extension (vendorName="jpox", key="key-auto-increment" ,value="true")

或者,将 id 更改为字符串,您的原始代码即可工作。

我无法使用 gae.pk-id 让 Long PK 与 datanucleus 一起使用。

This works with your original code (ie., Long id):

@Extension (vendorName="jpox", key="key-auto-increment" ,value="true")

Or, change id to String and your orig code works.

I could not get Long PK to work with datanucleus using gae.pk-id.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文