Google App Engine for Java で CRUD
次はGAE/Jを使ったAndroidアプリを作りたいので、GAE/Jの勉強中。
とりあえず簡単なCRUD作ってみました。
ソース
PMF.java
PersistenceManagerFactoryを保持するユーティリティクラス
import javax.jdo.JDOHelper; import javax.jdo.PersistenceManager; import javax.jdo.PersistenceManagerFactory; public class PMF { private static final PersistenceManagerFactory INSTANCE = JDOHelper.getPersistenceManagerFactory("transactions-optional"); public static final PersistenceManagerFactory get() { return INSTANCE; } public static final PersistenceManager createManager() { return get().getPersistenceManager(); } private PMF() { } }
Content.java
コメントを保持するエンティティクラス
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 com.google.appengine.api.datastore.Key; @PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true") public class Content { @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) @PrimaryKey private Key key; @Persistent private String title; @Persistent private String comment; public Content() { } public Content(Key key, String title, String comment) { this.key = key; this.title = title; this.comment = comment; } public Key getKey() { return key; } public void setKey(Key key) { this.key = key; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getComment() { return comment; } public void setComment(String comment) { this.comment = comment; } }
CommentDAO.java
CRUDするクラス
import java.util.ArrayList; import java.util.Collection; import java.util.List; import javax.jdo.PersistenceManager; import javax.jdo.annotations.Transactional; public class CommentDAO{ @Transactional public boolean addContent(Content content) { PersistenceManager pm = PMF.createManager(); try { pm.makePersistent(content); return true; } catch (Exception e) { return false; } finally { pm.close(); } } @Transactional public boolean updateContent(Content content) { PersistenceManager pm = PMF.createManager(); try { Content persistenctContent = pm.getObjectById(Content.class, content.getKey()); persistenctContent.setTitle(content.getTitle()); persistenctContent.setComment(content.getComment()); return true; } catch (Exception e) { return false; } finally { pm.close(); } } @Transactional public boolean deleteContent(Integer id) { PersistenceManager pm = PMF.createManager(); try { Content persistenctContent = pm.getObjectById(Content.class, id); pm.deletePersistent(persistenctContent); return true; } catch (Exception e) { return false; } finally { pm.close(); } } @Transactional @SuppressWarnings("unchecked") public List<Content> getContentList() { PersistenceManager pm = PMF.createManager(); try { String query = "select from " + Content.class.getName(); Collection<Content> result = (Collection<Content>) pm.newQuery(query).execute(); return new ArrayList(pm.detachCopyAll(result)); } finally { pm.close(); } } }
とりあえず
動きます。
おかしい所があったら教えてください。
参考
Google App Engine for Java [実践]クラウドシステム構築
まだデータストアの最初くらいしか読んでないですが、とても参考になります。