如何从playframework中的超类inheritance模型

我试图了解inheritance如何发挥作用! 但尚未成功。

所以,我有这样的超类:

@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个inheritance的类:

 @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个inheritance类控制器:

 public class Subs and Sub1s extends CRUD { } 

应用程序启动后,我在MySQL数据库中为我的模型(Sub和Sub1)收到了2个表,其结构如下: id bigint(20), name varchar(255)。 没有在超类中的testVal

当我尝试在CRUD界面中创建Sub类的新对象时,我收到了这样的错误: 模板中出现执行错误{module:crud} /app/views/tags/crud/form.html。 引发的exception是MissingPropertyException:没有这样的属性:testVal for class:models.Sub。

在{module:crud} /app/views/tags/crud/form.html(64行左右) #{crud.numberField name:field.name,value:(currentObject?currentObject [field.name]:null)/}

  1. 如何正确生成inheritance模型的MySQL表并修复错误?
  2. 有几个inheritance类可以有一个superController吗?

好吧,多亏了sdespolit ,我做了一些实验。 这就是我所拥有的:

超类:

 @MappedSuperclass @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public abstract class SuperClass extends Model { } 

inheritance类:

 @Entity public class Sub extends SuperClass { } 

我以这种方式制作的“超级控制器”:

 @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)用于告诉拦截器应该使用哪个类

 public class SuperController extends Controller { @After/Before/Whatever public static void doSomething() { String actionMethod = request.actionMethod; Class model = getControllerAnnotation(CRUD.For.class).value(); List allowedActions = new ArrayList(); allowedActions.add("show"); allowedActions.add("list"); allowedActions.add("blank"); if (allowedActions.contains(actionMethod)) { List list = play.db.jpa.JPQL.instance.find(model.getSimpleName()).fetch(); } } } 

我不确定doSomething()方法真的很好,Java风格/ Play!风格。 但它对我有用。 请告诉我是否有可能以更原生的方式了解模型的课程。

“每个类的表和表是JPA规范的可选function,因此并非所有提供者都可以从WikiBook中支持它。

你为什么不使用@MappedSuperclass? 此外,您应该扩展GenericModel。 在您的示例中,您定义了两次id,这也可能是您遇到问题的原因。