Play Framework - How to inherit from a superclass?

I have a User class that extends Model and two classes that I want to extend for 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 objects must be accessible directly. To put it another way, I have users, clients, and drivers; Clients and drivers simply use all the user properties. Therefore, I need to have a valid user object, as well as a client and driver.

I need to get a list of all users (including clients and drivers).

I was not able to figure out how to make this work using ebean in Play. How can i do this?

+4
source share
2 answers

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

It is also possible to manually attach helper tables for drivers and clients using @OneToOne .

Using @OneToOne will also be an advantage of composition over inheritance, which is considered good practice.

+3
source

This might be a use case for the MappedSuperClass annotation. See http://docs.oracle.com/javaee/5/api/javax/persistence/MappedSuperclass.html for documentation. Presumably, this supports Ebean, although it is missing documentation.

+3
source

Source: https://habr.com/ru/post/1446860/


All Articles