且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从playframework中的超类继承模型

更新时间:2023-12-02 22:04:58

好吧,多亏了 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.