Created
December 4, 2009 09:54
-
-
Save shin1ogawa/248938 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 親エンティティを永続化しない状態で子エンティティを永続化できるか? | |
* <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