Skip to content

Instantly share code, notes, and snippets.

@shin1ogawa
Created December 4, 2009 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shin1ogawa/248938 to your computer and use it in GitHub Desktop.
Save shin1ogawa/248938 to your computer and use it in GitHub Desktop.
/**
* 親エンティティを永続化しない状態で子エンティティを永続化できるか?
* <p>Transactionなし</p>
* @throws EntityNotFoundException
*/
@Test
public void ajn3_01() throws EntityNotFoundException {
DatastoreService service = DatastoreServiceFactory.getDatastoreService();
KeyRange parentKeyRange = service.allocateIds("parent", 1);
Key parentKey = parentKeyRange.getStart();
KeyRange childKeyRange = service.allocateIds(parentKey, "child", 1);
Key childKey = childKeyRange.getStart();
@SuppressWarnings("unused")
Entity parent = new Entity(parentKey);
Entity child = new Entity(childKey);
service.put(child);
assertThat(service.get(childKey), is(not(nullValue())));
assertThat(service.prepare(new Query("child")).countEntities(), is(equalTo(1)));
assertThat(service.prepare(new Query("parent")).countEntities(), is(equalTo(0)));
}
/**
* 親エンティティを永続化しない状態で子エンティティを永続化できるか?
* <p>Transactionあり</p>
* @throws EntityNotFoundException
*/
@Test
public void ajn3_02() throws EntityNotFoundException {
DatastoreService service = DatastoreServiceFactory.getDatastoreService();
KeyRange parentKeyRange = service.allocateIds("parent", 1);
Key parentKey = parentKeyRange.getStart();
KeyRange childKeyRange = service.allocateIds(parentKey, "child", 1);
Key childKey = childKeyRange.getStart();
@SuppressWarnings("unused")
Entity parent = new Entity(parentKey);
Entity child = new Entity(childKey);
Transaction tx = service.beginTransaction();
service.put(tx, child);
tx.commit();
assertThat(service.get(childKey), is(not(nullValue())));
assertThat(service.prepare(new Query("child")).countEntities(), is(equalTo(1)));
assertThat(service.prepare(new Query("parent")).countEntities(), is(equalTo(0)));
Entity child2 = service.get(childKey);
tx = service.beginTransaction();
child2.setProperty("prop", "a");
service.put(tx, child2);
tx.commit();
}
/**
* 親エンティティを永続化しない状態で子エンティティを永続化できるか?
* <p>Transactionあり, KeyFactory#createKey()でやってみた</p>
* @throws EntityNotFoundException
*/
@Test
public void ajn3_03() throws EntityNotFoundException {
DatastoreService service = DatastoreServiceFactory.getDatastoreService();
Key parentKey = KeyFactory.createKey("parent", "a");
Key childKey = KeyFactory.createKey(parentKey, "child", "a");
@SuppressWarnings("unused")
Entity parent = new Entity(parentKey);
Entity child = new Entity(childKey);
Transaction tx = service.beginTransaction();
service.put(tx, child);
tx.commit();
assertThat(service.get(childKey), is(not(nullValue())));
assertThat(service.prepare(new Query("child")).countEntities(), is(equalTo(1)));
assertThat(service.prepare(new Query("parent")).countEntities(), is(equalTo(0)));
Entity child2 = service.get(childKey);
tx = service.beginTransaction();
child2.setProperty("prop", "a");
service.put(tx, child2);
tx.commit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment