Play Framework - 如何从超类继承? [英] Play Framework - How to inherit from superclass?
问题描述
我有一个扩展Model的User类,以及我想扩展User类的两个类。
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).
我无法弄清楚如何在Play中使用 ebean
来完成这项工作。我怎样才能做到这一点?
I haven't been able to figure out how to make this work using ebean
in Play. How can I do this?
推荐答案
为了保持用户
表的具体,您可以使用 @Inheritance
注释。有关讨论,请参见 Play Framework 2 Ebean和InheritanceType为JOINED 关于那个。
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.
也可以使用 @OneToOne
为司机和客户手动加入辅助表。
Also possible is to manually join auxiliar tables for Drivers and Customers using @OneToOne
.
使用 @OneToOne
也有利于作品优于继承,这被认为是一种很好的做法。
Using @OneToOne
would also be favor composition over inheritance, which is considered a good practice.
这篇关于Play Framework - 如何从超类继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!