How to inherit a model from superclass in playframework

删除回忆录丶 提交于 2019-12-03 15:04:19

Well, thanks to sdespolit, I've made some experiments. And here is what I've got:

Superclass:

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

Inherited class:

@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) 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();
        }
    }
}

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.

"and table per class is an optional feature of the JPA spec, so not all providers may support it" from WikiBook.

Why don't you use @MappedSuperclass? Furthermore you should extend GenericModel. In your example you defined id twice, which could be the reason of you problem too.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!