Play Framework - How to inherit from superclass?

a 夏天 提交于 2019-12-21 19:39:31

问题


I have a User class, which extends Model, and two classes that I want to extend the User class.

User.java:

@Entity
@Table(name = "users")
public class User extends Model implements RoleHolder {

    private static final long serialVersionUID = 1L;

    @Id
    public Long id;

...

Driver.java:

public class Driver extends User {
...

Customer.java:

public class Customer extends User {
...

Edit All three entities need to be directly accessed. To say it another way, I have Users, Customers, and Drivers; Customers and Drivers just happen to share all of the properties of a User. Therefore, I need to have a valid User entity as well as Customer and Driver.

I need to be able to get a list of all Users (including Customers and Drivers).

I haven't been able to figure out how to make this work using ebean in Play. How can I do this?


回答1:


To keep the User table concrete, you can use the @Inheritance annotation. See Play Framework 2 Ebean and InheritanceType as JOINED for a discussion about that.

Also possible is to manually join auxiliar tables for Drivers and Customers using @OneToOne.

Using @OneToOne would also be favor composition over inheritance, which is considered a good practice.




回答2:


This might be an use case for the MappedSuperClass annotation. See http://docs.oracle.com/javaee/5/api/javax/persistence/MappedSuperclass.html for documentation. Presumably, this is suppoerted by Ebean, although there's a lack of documentation about it.



来源:https://stackoverflow.com/questions/13447441/play-framework-how-to-inherit-from-superclass

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