[GAE/J]同一型の子Entityを持つと、子Entityの判断がつかない

15 views
Skip to first unread message

mokkouyou

unread,
May 20, 2009, 1:47:52 AM5/20/09
to Google-App-Engine-Japan
はじめまして。mokkouyouです。

現在GAE/JにてJDO経由でEntity操作をしていますが、

別フィールドで複数の子Entityを持つ親Entityを取得してみると、
全ての子Entityが同じインスタンスとなってしまいます。

※DataViewrでみる限りは全ての子Entityが登録されているのですが・・・
Rainbow1
以下ざっくりとしたソーステストになります。
単体テストの親クラスとEntityはサンプルより流用しました。

利用方がまずい等ありましたら御教授いただけたらと思います。
※問題発覚のソースでは親もKeyとして、アプリケーションよりKeyを設定していました。

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Employee {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long id;

@Persistent
private ContactInfo myContactInfo;
@Persistent
private ContactInfo officeContactInfo;
//setter/getter
}
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class ContactInfo {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;

@Persistent
private String streetAddress;

@Persistent
private String city;

@Persistent
private String stateOrProvince;

@Persistent
private String zipCode;
//setter/getter
}
public class EmployeeTest extends LocalDatastoreTestCase {
public void testEmployeeFind() {
Employee e = new Employee();

ContactInfo ci = new ContactInfo();
ci.setZipCode("1111");
e.setMyContactInfo(ci);

ContactInfo office = new ContactInfo();
office.setZipCode("2222");
e.setOfficeContactInfo(office);

Transaction tx = pm.currentTransaction();
try {
tx.begin();
pm.makePersistent(e);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
}

Query query = pm.newQuery(Employee.class);
query.setRange(0, 1);
List<Employee> results = (List<Employee>) query.execute();
if(results.size() > 0) {
Employee target = results.get(0);
assertNotSame(target.getMyContactInfo(), target.getOfficeContactInfo
());//ここで失敗
assertEquals("1111", target.getMyContactInfo().getZipCode());
assertEquals("2222", target.getOfficeContactInfo().getZipCode());
} else {
fail();
}

}


PersistenceManagerFactory factory;
PersistenceManager pm;

@Override
public void tearDown() throws Exception {
super.tearDown();
if(!pm.isClosed()) {
pm.close();
}
}

@Override
public void setUp() throws Exception {
super.setUp();
factory = JDOHelper.getPersistenceManagerFactory("transactions-
optional");
pm = factory.getPersistenceManager();
}
}

Shinichi Ogawa

unread,
May 20, 2009, 9:40:39 AM5/20/09
to google-app-...@googlegroups.com
mokkouyouさん

自分も同じ事で悩んだ事がありましたが、結局以下のようにして目的を実現する事にしています。

例でいうEmployeeクラス内で@Persistent ContactInfoを複数保持するのではなく、

class Entity
@Persistent Key myContactInfoKey
@Persistent Key officeContactInfoKey
...

のように、Employee側でContactInfoのKeyを保持してContactInfoの実体は別途取得する。

または、

class Employee
@Persistent MyContactInfo myContactInfo
@Persistent OfficeContactInfo officeContactInfo
...

class MyContactInfo
@Persistent ContactInfo contactInfo
...

class OfficeContactInfo
@Persistent ContactInfo contactInfo
...

のように、関連の目的ごとに別Entityを作って保持する。

GAEのデータストアでは、
親Entityが子Entityのキーをフィールドとして保持していないのかな?と思ってます。

2009/5/20 mokkouyou <mokk...@gmail.com>:

mokkouyou

unread,
May 20, 2009, 11:09:49 AM5/20/09
to Google-App-Engine-Japan
mokkouyouです。

なるほど・・・一枚かぶせると平気だったりするわけですね。
無難なのはやはりKeyの保持でしょうか。
でもなんかもったいない気もしますね。


一応私のほうでは、@Embededdアノテーションを付与して
埋め込みオブジェクトとすることで複数の子Entityを認識できることは確認できましたが、
親に専用の子となってしまうので、マスタ系の場合には厳しいですね。

なんとなくバグの疑いがありそうな挙動ですね(^^;

On 5月20日, 午後10:40, Shinichi Ogawa <shin1og...@gmail.com> wrote:
> mokkouyouさん
>
> 自分も同じ事で悩んだ事がありましたが、結局以下のようにして目的を実現する事にしています。
>
> 例でいうEmployeeクラス内で@Persistent ContactInfoを複数保持するのではなく、
>
> class Entity
> @Persistent Key myContactInfoKey
> @Persistent Key officeContactInfoKey
> ...
>
> のように、Employee側でContactInfoのKeyを保持してContactInfoの実体は別途取得する。
>
> または、
>
> class Employee
> @Persistent MyContactInfo myContactInfo
> @Persistent OfficeContactInfo officeContactInfo
> ...
>
> class MyContactInfo
> @Persistent ContactInfo contactInfo
> ...
>
> class OfficeContactInfo
> @Persistent ContactInfo contactInfo
> ...
>
> のように、関連の目的ごとに別Entityを作って保持する。
>
> GAEのデータストアでは、
> 親Entityが子Entityのキーをフィールドとして保持していないのかな?と思ってます。
>
> 2009/5/20 mokkouyou <mokkou...@gmail.com>:

mokkouyou

unread,
May 27, 2009, 7:18:12 AM5/27/09
to Google-App-Engine-Japan
mokkouyouです

自己レスではありますが、

>親に専用の子となってしまうので、マスタ系の場合には厳しいですね。

マスタ系の場合にはそもそも、unownedなので、Keyを保持ですね。
Reply all
Reply to author
Forward
0 new messages