如何从playframework中的超类继承模型 [英] How to inherit a model from superclass in playframework
问题描述
我试图了解继承如何发挥作用!但是还没有成功。
I'm trying to understand how does the inheritance work in play! But unsuccessfully yet.
所以,我有这样的超类:
So, I have such superclass:
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
abstract class SuperClass extends Model {
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "SEQ_TABLE")
@TableGenerator(name = "SEQ_TABLE")
Long id;
int testVal;
}
2个继承的类:
@Entity
public class Sub extends SuperClass {
String name;
@Override
public String toString() {
return name;
}
}
@Entity
public class Sub1 extends SuperClass {
String name;
@Override
public String toString() {
return name;
}
}
此外,我还有2个用于继承类的控制器:
Also I have 2 controllers for inherited classes:
public class Subs and Sub1s extends CRUD {
}
应用程序启动后,我在MySQL数据库中为我的模型(Sub和Sub1)收到了2个表格,结构如下: id bigint(20),名称 varchar(255)。没有 testVal ,它位于超类中。
After application was started, I recieve 2 tables in MySQL db for my models (Sub and Sub1) with such structure: id bigint(20), name varchar(255). Without testVal which is in superclass.
当我尝试在CRUD界面中创建 Sub 类的新对象时我收到了这样的错误:
模板{module:crud} /app/views/tags/crud/form.html中发生了执行错误。引发的异常是 MissingPropertyException:没有这样的属性:testVal for class:models.Sub。
And when I try to create new object of Sub class in CRUD interface I recieve such error: Execution error occured in template {module:crud}/app/views/tags/crud/form.html. Exception raised was MissingPropertyException : No such property: testVal for class: models.Sub.
In {module:crud} / app / views / tags / crud / form.html(第64行)
#{crud.numberField名称:field.name,value :( currentObject?currentObject [field.name]:null)/}
In {module:crud}/app/views/tags/crud/form.html (around line 64) #{crud.numberField name:field.name, value:(currentObject ? currentObject[field.name] : null) /}
- 如何正确生成继承模型的MySQL表并修复错误?
- 是否可以为几个继承的类设置一个superController?
推荐答案
好吧,多亏了 sdespolit ,我做了一些实验。这就是我所拥有的:
Well, thanks to sdespolit, I've made some experiments. And here is what I've got:
超类:
@MappedSuperclass
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class SuperClass extends Model {
}
继承类:
@Entity
public class Sub extends SuperClass {
}
我以这种方式制作的超级控制器:
"Super Controller" I made in such way:
@With({Secure.class, SuperController.class})
@CRUD.For(Sub.class)
public class Subs extends CRUD {
}
@With({Secure.class, SuperController.class})
@CRUD.For(Sub1.class)
public class Sub1s extends CRUD {
}
@ CRUD.For(Sub.class)用于告诉拦截器它应该工作的类
@CRUD.For(Sub.class) is used to tell the interceptors with what class it should work
public class SuperController extends Controller {
@After/Before/Whatever
public static void doSomething() {
String actionMethod = request.actionMethod;
Class<? extends play.db.Model> model = getControllerAnnotation(CRUD.For.class).value();
List<String> allowedActions = new ArrayList<String>();
allowedActions.add("show");
allowedActions.add("list");
allowedActions.add("blank");
if (allowedActions.contains(actionMethod)) {
List<SuperClass> list = play.db.jpa.JPQL.instance.find(model.getSimpleName()).fetch();
}
}
}
我不确定 doSomething()方法非常好用Java风格/ Play!风格。但它对我有用。
请告诉我是否有可能以更本土的方式了解模特的课程。
I'm not sure about doSomething() approach is truly nice and Java-style/Play!-style. But it works for me. Please tell me if it's possible to catch out the model's class in more native way.
这篇关于如何从playframework中的超类继承模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!