通過 GWT-RPC 發送持久的 JDO 實例 (Sending persisted JDO instances over GWT-RPC)


問題描述

通過 GWT-RPC 發送持久的 JDO 實例 (Sending persisted JDO instances over GWT-RPC)

I've just started learning Google Web Toolkit and finished writing the Stock Watcher tutorial app.

Is my thinking correct that if one wants to persist a business object (like a Stock) using JDO and send it back and forth to/from the client over RPC then one has to create two separate classes for that object: One with the JDO annotations for persisting it on the server and another which is serialisable and used over RPC?

I notice the Stock Watcher has separate classes and I can theorise why:

  • Otherwise the gwt compiler would try to generate javascript for everything the persisted class referenced like JDO and com.google.blah.users.User, etc
  • Also there may be logic on the server-side  class which doesn't apply to the client  and vice-versa.

I just want to make sure I'm understanding this correctly. I don't want to have to create two versions of all my business object classes which I want to use over RPC if I don't have to.


參考解法

方法 1:

The short answer is: you don't need to create duplicate classes.

I recommend that you take a look from the following google groups discussion on the gwt-contributors list:

http://groups.google.com/group/google-web-toolkit-contributors/browse_thread/thread/3c768d8d33bfb1dc/5a38aa812c0ac52b

Here is an interesting excerpt:

  

If this is all you're interested in, I   described a way to make GAE and    GWT-RPC work together "out of the   box". Just declare your entities as:    @PersistenceCapable(identityType =   IdentityType.APPLICATION,  detachable   = "false")  public class MyPojo implements Serializable {  } 

     

and everything will work, but you'll   have to manually deal with    re-attachment when sending objects   from the client back to the server.

You can use this option, and you will not need a mirror (DTO) class. You can also try gilead (former hibernate4gwt), which takes care of some details within the problems of serializing enhanced objects.

方法 2:

Your assessment is correct. JDO replaces instances of Collections with their own implementations, in order to sniff when the object graph changes, I suppose. These implementations are not known by the GWT compiler, so it will not be able to serialize them. This happens often for classes that are composed of otherwise GWT compliant types, but with JDO annotations, especially if some of the object properties are Collections.

For a detailed explanation and a workaround, check out this pretty influential essay on the topic: http://timepedia.blogspot.com/2009/04/google-appengine-and-gwt-now-marriage.html

方法 3:

I finally found a solution. Don't change your object at all, but for the listing do it this way:

List<YourCustomObject> secureList=(List<YourCustomObject>)pm.newQuery(query).execute();
return new ArrayList<YourCustomObject>(secureList);

The actual problem is not in Serializing the Object... the problem is to Serialize the Collection class which is implemented by Google and is not allowed to Serialize out.

方法 4:

You do not have to create two versions of the domain model.

Here are two tips:

Use a String encoded key, not the Appengine Key class.

pojo = pm.detachCopy(pojo)

...will remove all the JDO enhancements.

方法 5:

You don't have to create separate instances at all, in fact you're better off not doing it. Your JDO objects should be plain POJOs anyway, and should never contain business logic. That's for your business layer, not your persistent objects themselves.

All you need to do is include the source for the annotations you are using and GWT should compile your class just fine. Also, you want to avoid using libraries that GWT can't compile (like things that use reflection, etc.), but in all the projects I've done this has never been a problem.

(by CodeAndCatsMiguel Pinguser130846Anurag VermaNick Siderakisrustyshelf)

參考文件

  1. Sending persisted JDO instances over GWT-RPC (CC BY-SA 3.0/4.0)

#gwt #gwt-rpc #jdo






相關問題

通過 GWT-RPC 發送持久的 JDO 實例 (Sending persisted JDO instances over GWT-RPC)

GWT:帶有 db 變量的屬性文件的安全位置 (GWT: Safe place for properties file with db variables)

paket khusus dalam toples eksternal GWT (specific packages in a external jar GWT)

在 gwt 框架中使用參數更改 url (Changing url with parameters in gwt framework)

通過 JavaScript 在標題或 HTML 中動態包含腳本元素 (Dynamically include script element in header or HTML by JavaScript)

Java如何在構造函數中將接口作為參數傳遞? (Java How do I pass interface as parameter in Constructor?)

如何更改 gwt 面板的內容? (How to change the content of a gwt panel?)

內容類型是“應用程序/x-www-form-urlencoded”。預期的“文本/x-gwt-rpc” (Content-Type was 'application/x-www-form-urlencoded'. Expected 'text/x-gwt-rpc')

如何從谷歌應用引擎調用我的應用網址 (How to call my app url from google app engine)

GXT NumberField 不可編輯 (GXT NumberField not editable)

在碼頭上部署 gwt Web 應用程序 (deploying gwt web application on jetty)

在 gwt 中使用 html5 畫布畫一個圓圈? (draw a circle using html5 canvas in gwt?)







留言討論