これが私のモデルの親です:
abstract public class ApiModel {
// Problem 1
public static ExpressionList<Model> find() {
return null;
}
public static <T extends Model> T findById(Long id) {
return null;
}
}
モデル :
public class MyModel extends ApiModel {
private static Model.Finder<Long,MyModel> find = new Model.Finder(Long.class, MyModel.class);
public static ExpressionList<MyModel> find() {
return find.where();
}
public static MyModel findById(Long id) {
return find.byId(id);
}
}
親のコントローラー:
public class ApiController<T extends ApiModel> extends Controller {
public Result list() {
// Problem 2
ExpressionList<T> list = T.find();
return ok(Json.toJson(list.orderBy("name"), 10));
}
public Result create() {
return update(null);
}
public Result details(Long id) {
// Problem 3
T model = T.findById(id);
// ...
return ok(result);
}
public Result update(Long id) {
// Problem 4
Form<T> form = form(T.class).bindFromRequest();
T model = form.get();
// Problem 5
T.save();
// ...
return ok(result);
}
public Result delete(Long id) {
// ...
return ok(result);
}
}
コントローラー
public class AController extends ApiController<MyModel> {
public final static AController rest = new AController();
private AController() {}
}
私が直面している問題:
find()
を返す必要がありますExpressionList<T extends Model>
が、これを入れるとエラーになります。- Pb1はこのエラーを表示し、「タイプの不一致:ExpressionListからExpressionListに変換できません」と表示します。1.、2。を修正することで修正されると思います。
- これは奇妙で、「境界の不一致:ApiModel型のジェネリックメソッドfindById(Long)は引数(Long)には適用できません。推定された型T&Modelは境界パラメータの有効な代替ではありません」を返します。
- もちろん
.class
、これは使えません。しかし、どうすればよいでしょうか? - モデルは注釈を使用し
@Entity
ているため、ここでは使用できません。認識されません:/
私はすべてが関連していると思います。たぶん私は自分のコードをうまく設計していませんか?
これがそのような構造の理由です。私は(静的コントローラーを備えた)PlayFrameworkを使用しており、継承、つまり汎用モデルを実行するのが好きです。しかし、そのためには、静的参照ではなくインスタンスが必要です。したがって、public final static AController rest
。しかし、その後、モデル(find
&findById
)の静的コンテキストにアクセスできません。そこで、ApiModelを作成しました。しかし、それ以上の助けにはなりません。