More Related Content
Similar to Appengine Java Night #2a
Similar to Appengine Java Night #2a (20)
Appengine Java Night #2a
- 2. ...shin1ogawa
Google, Java, AppEngine, Eclipse,
Wicket, Maven, Hudson, ...
• OSS
• [SF.jp]Jiemamy...
• [SF.jp]gae-j-samples...GAE/J GAE/J/Maven
• [SF.jp]gaejtools...GAE/J
• [SF.jp]asclipse...Amateras AIR GEAR AS3
• java-ja, wicket-ja,
(http://www.topgate.co.jp)
GoogleAppEngine GoogleApps
- 4. ) Key
• Key EntityGroup
• Entity EntityGroup …
• Key
• ApplicationID
• Entity Key, Kind
• name or long
•
- 5. ) KeyFactory
• createKey() Key
• keyToString() Key
• stringToKey()
Key
- 6. ) Entity
• setProperty(“ ”, Object )
• Object getProperty(“ ”)
• boolean hasProperty(“ ”)
• <missing>
•
• HashSet Collection ArrayList
• int Long…
- 7. ) DatastoreService
•
• Key put(Entity)
• List<Key> put(Iterable<Entity>)
•
• void delete(Key... keys)
• void delete(Iterable<Key>)
•
Transaction
- 8. ) DatastoreService
•
• Entity get(Key key)
• Key
EntityNotFoundException
• Map<Key, Entity> get(
Iterable<Key> key)
• Key
Map
- 10. ) DatastoreService
• 1.2.5 Key
Entity Key
1.2.5
Key
• KeyRange allocalteIds(
[ Key], Kind, )
• KeyRange
• getStart(), getEnd(), iterator()
- 11. Datastore Service
(Entity )
KeyRange parentKeys =
service.allocateIds("Parent", 1);
Key parentKey =
parentKeys.getStart();
KeyRange childKeys =
service.allocateIds(parentKey, "Child", 2);
Iterator<Key> childKeysIterator =
childKeys.iterator();
Entity parent = new Entity(parentKey);
Entity child1 = new Entity(childKeys.next());
Entity child2 = new Entity(childKeys.next());
- 12. Datastore Service
( )
Transaction tx = service.beginTransaction();
try {
List<Entity> entities =
Arrays.asList(parent, child1, child2
service.put(tx, entities);
tx.commit();
} finally {
if (tx.isActive()) { tx.rollback(); }
}
- 14. Datastore Service
Query
• new Query(String kind)
• JDO
• new Query(String kind, Key ancestorKey)
• Kind
• new Query(Key ancestorKey)
• Kind
•
- 15. Datastore Service
Query
•
• Query addFilter(String propertyName,
FilterOperator operator, Object value)
•
• Query addSort(String propertyName,
SortDirection direction)
•
• Query setKeysOnly()
• Key
Entity.KEY_RESERVED_PROPERTY
- 17. Datastore Service
PreparedQuery
•
• List<Entity> asList(FetchOptions)
•
• Iterator<Entity> asIterator([FetchOptions])
•
• Iterable<Entity> asIterable([FetchOptions])
• iterator() Datastore
asIterator()
• FetchOptions
• offset, limit
- 18. Datastore Service
Query
Query query = new Query(“Entity”);
query.addFilter(“name”,
FilterOperator.GREATER_THAN_OR_EQUAL, “hoge”)
.addSort("name", SortDirection.ASCENDING)
.addSort("__key__", SortDirection.ASCENDING);
DatastoreService service =
DatastoreServiceFactory.getDatastoreService();
List<Entity> entities =
service.prepare(query).asList(
FetchOptions.Builder.withOffset(0).limit(100));
- 19. Datastore Service
Query : EntityGroup
Iterator<Entity> entityGroup =
service.prepare(new Query(rootKey)
.addSort(“__key__”)).asIterator(
FetchOptions.Builder.withOffset(0).limit(100));
Parent parent = null;
while (entityGroup.hasNext) {
Entity entity = entityGroup.next();
if (entity.getKind().equals(“Parent”))
parent = EntityUtil.toBean(entity,Parent.class);
else if (entity.getKind().equals(“Child”))
parent.getChidren().add(
EntityUtil.toBean(entity, Child.class);
...
- 20. Datastore Service
JDO
JDO
class MyEntity {
List<Child> children1;
List<Child> children2;
}
myEntity.getChildren1().addAll(childA, childB);
myEntity.getChildren2().addAll(childC, childD);
manager.makePersistent(myEntity);
List
- 21. Datastore Service
JDO
MyEntity myEntity =
manager.getObjectById(MyEntity.class, key);
List<Child> children1 =
myEntity.getChildren1();
List<Child> children2 =
myEntity.getChildren2();
children1 childA childB
childC childD children2 null
- 22. Datastore Service
JDO
MyEntiy(1) Child
(A-D)
MyEntiy(1)/Child(A)
MyEntiy(1)/Child(B)
MyEntiy(1)/Child(C)
MyEntiy(1)/Child(D)