2

私はいくつかのドキュメントを確認しましたが、まだデータストアと通信できません...GWT Webアプリで使用されるobjectifyのサンプルプロジェクト/コードを誰かに教えてもらえますか(私はEclipseを使用しています)...ただの単純な ' RPC を使用した put および 'get' アクションで実行する必要があります...または、少なくともその方法を教えてください

4

1 に答える 1

3

objectifyを機能させる方法を理解する最も簡単な方法は、DavidのChandlerブログからこの記事で説明されているすべての手順を繰り返すことです。GWT、GAE(Java)、gwt-presenter、gin \ guiceなどに興味がある場合は、ブログ全体を読む必要があります。そこには実用的な例がありますが、とにかくここでは少し高度な例を示します。

パッケージsharedでエンティティ/モデルを定義します。

import javax.persistence.Embedded;
import javax.persistence.Id;
import com.google.gwt.user.client.rpc.IsSerializable;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Unindexed;

@Entity
public class MyEntry implements IsSerializable {
    // Objectify auto-generates Long IDs just like JDO / JPA
    @Id private Long id;
    @Unindexed private String text = "";
    @Embedded private Time start;

    // empty constructor for serialization
    public MyEntry () {
    }
    public MyEntry (Time start, String text) {
        super();
        this.text = tText;
        this.start = start;
    }
    /*constructors,getters,setters...*/
}

時間クラス(sharedパッケージも)には、フィールドミリ秒が1つだけ含まれています。

@Entity  
public class Time implements IsSerializable, Comparable<Time> {
protected int msecs = -1;    
  //rest of code like in MyEntry 
}

ObjectifyDao上記のリンクからserver.daoパッケージにクラスをコピーします。次に、MyEntry専用のDAOクラスを作成します--MyEntryDAO:

package com.myapp.server.dao;

import java.util.logging.Logger;

import com.googlecode.objectify.ObjectifyService;
import com.myapp.shared.MyEntryDao;

public class MyEntryDao extends ObjectifyDao<MyEntry>
{
    private static final Logger LOG = Logger.getLogger(MyEntryDao.class.getName());

    static
    {
        ObjectifyService.register(MyEntry.class);
    }

    public MyEntryDao()
    {
        super(MyEntry.class);
    }

}

最後に、database(serverpackage)にリクエストを送信できます。

public class FinallyDownloadingEntriesServlet extends HttpServlet {
      protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws
ServletException, IOException {
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/plain");
                //more code...
                resp.setHeader("Content-Disposition", "attachment; filename=\""+"MyFileName"+".txt\";");
        try {
            MyEntryDao = new MyEntryDao();
          /*query to get all MyEntries from datastore sorted by start Time*/
            ArrayList<MyEntry> entries = (ArrayList<MyEntry>) dao.ofy().query(MyEntry.class).order("start.msecs").list();

            PrintWriter out = resp.getWriter();
            int i = 0;
            for (MyEntry entry : entries) {
                ++i;
                out.println(i);
                out.println(entry.getStart() + entry.getText());
                out.println();
            }
        } finally {
            //catching exceptions
        }
    }
于 2011-03-20T21:19:52.810 に答える