使用rpc将gwt实体与jdo保存到Google应用程序引擎数据存储中 [英] Save gwt entities to google application engine datastore with jdo, using rpc

查看:118
本文介绍了使用rpc将gwt实体与jdo保存到Google应用程序引擎数据存储中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,iam是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实现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(){}

Student 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; 

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


$ b 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

...

字符串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);
返回学生保存 - 确定;
}

}

PMF

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

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

private PMF(){$​​ b $ b}

public static PersistenceManagerFactory get(){
return pmfInstance;




  ... 

private final GreetingServiceAsync greetingService = GWT
。创建(GreetingService.class);

greetingService.greetServer(greet,
new AsyncCallback< String>(){
public void onFailure(Throwable caught){
//显示RPC错误给用户的消息
}

public void onSuccess(String result){
//显示成功消息
}
});

greetingService.saveStudent(新学生(kostas,trichas),
新增AsyncCallback< String>(){
public void onFailure(Throwable caught){
//向用户显示RPC错误消息

$ b public void onSuccess(String result){
//显示成功消息
}
});

...

上述实施是否正确?我将这个示例应用程序部署到gae,并且它没有保留对象 student (您可以在gae datastore查看器中浏览实体)



<请检查它:

http://gwtgaedatastore.appspot。

解决方案

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

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

解决方案

Change your int studentID to Long id to get it working

这篇关于使用rpc将gwt实体与jdo保存到Google应用程序引擎数据存储中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆