タイプとは、次のことを可能にするものです。
public class AnyObject{
List<this.type> list;
}
私は次のことがうまくいかないことを知っています。
public class AnyObject{
List<this.getClass()> list;
}
では、たとえば酒など、これが何であれ、タイプのリストを作成するにはどうすればよいでしょうか。
- - - - - - - - アップデート - - - - - - - -
申し訳ありませんが、私は明確ではなかったと思います。タイプの消去を回避する方法はないようですが、私の問題を解決する方法がまだある場合は、もっと詳しく説明します。開示、これはObjectifyの質問です。すみません、今見に来ました。
ほら、できる限り明確に...
Objectiy を使用して GAE データストアに保持する予定のすべてのエンティティについてKey<?>
、id フィールドと親フィールドを使用して Objectify を生成するメソッドが必要です。このメソッドを呼び出しましょうgenerateKey()
。これがどのように見えるかです。
public Key<MyEntity> generateKey() {
Key<MyEntity> key = Key.create(this.parent, MyEntity.class, this.id);
return key;
}
問題は、作成するすべてのエンティティに対して、多かれ少なかれ、この正確なコードを書かなければならないことです。実は他にも繰り返しコードはありますが、私の言いたいことは、この繰り返しコードだけで成り立ちます。
だから私はこれを試しました。MyProjectEntity というクラスを作成し、すべてのエンティティにそれを拡張させました。generateKey()
次に、ジェネリックを使用してメソッドを実装しました。
public abstract class MyProjectEntity<T, Y> {
@Id Long id;
@Parent Key<T> parentKey;
public Key<Y> generateKey() {
Key<Y> key = Key.create(this.parentKey, this.getClass(), this.id);
return key;
}
}
次に、作成した MyProjectEntity という新しいクラスを使用して、すべてのエンティティ クラスを拡張しました。そんな...
@Entity
public class MyEntity extends MyProjectEntity<MyEntityParent> {...}
これで、すべてのエンティティにgenerateKey()
メソッドが追加されましたが、うまくいきませんでした。Objectify は私に怒鳴り、IllegalArgumentException と言いました。T 型の Key を宣言できません。
それから試してみKey<Object>
ましたが、Objectify はまだ満足していませんでした。Objectify は、Object は登録されたエンティティではないと言いました。オブジェクトを登録する必要があります!?!? そして、それは、Objectify が提供する型付きキーの要点をすべて失ってしまいます。
良い解決策はありますか。ありがとう!
-- 更新 2 --
誰かが Key.create(myEntity) を指摘したので、私は私の完全な使用を指摘する必要があります...
/**********************************************************************************************************************
* Constructors END & Identification and Relationship Methods BEGIN
**********************************************************************************************************************/
@ApiSerializationProperty(name = "id")
public String getWebSafeKey() {
String webSafeKey = getKey().getString();
return webSafeKey;
}
public void setWebSafeKey(String webSafeKey) throws BadRequestException {
try {
Key<MyEntity> key = Key.create(webSafeKey);
setKey(key);
} catch (IllegalArgumentException illegalArgumentException) {
throw new BadRequestException(ErrorMessage.INVALID_ID);
}
}
@ApiSerializationProperty(name = "parentId")
public String getParentWebSafeKey() {
String webSafeKey = parent.getString();
return webSafeKey;
}
public void setParentWebSafeKey(String parentWebSafeKey) throws BadRequestException {
if (id == null) {
try {
parent = Key.create(parentWebSafeKey);
} catch (IllegalArgumentException illegalArgumentException) {
throw new BadRequestException(ErrorMessage.invalidParentId("Property"));
}
} else {
/* Do nothing. Only set parent here if setWebSafeKey is never called, such as during a create. */
}
}
@ApiSerializationProperty(ignored = AnnotationBoolean.TRUE)
public Key<MyEntity> getParentKey() {
return parent;
}
public void setParentKey(Key<MyEntity> parentKey) {
this.parent = parentKey;
}
@ApiSerializationProperty(ignored = AnnotationBoolean.TRUE)
public Key<MyEntity> getKey() {
Key<MyEntity> key = Key.create(parent, MyEntity.class, id);
return key;
}
public void setKey(Key<MyEntity> key) {
id = key.getId();
parent = key.getParent();
}
public boolean webSafeKeyEquals(String webSafeKey) {
boolean equals;
if (id !=null & parent !=null) {
equals = getWebSafeKey().equals(webSafeKey);
} else {
equals = false;
}
return equals;
}
/**********************************************************************************************************************
* Identification Methods END & Other Getters and Setters BEGIN
**********************************************************************************************************************/
MyEntity を実際のエンティティ名に置き換えて、作成するすべてのエンティティにこれをすべて挿入する必要があります。タイピングだけではありません。このコードはエンティティ クラスに属していませんが、抽象的な親に属しています。クラス内の特定のエンティティに固有のコードのみを使用できれば、モデルはよりクリーンになり、拡張が容易になります。再度、感謝します。