Home > Software design >  I have a problem with spring boot inheritance classes
I have a problem with spring boot inheritance classes

Time:07-03

I have a user class which is a supper class and a student class a subclass of users. I don’t have an error in the code but I don't why it does not make tables in the database when I remove "extends user" it creates the table?

here is my code

@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Data
public abstract class User {
    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Long id;
    private String username;
    private String email;
    private String password;

    @Enumerated(EnumType.STRING)
    @Column(length = 20)
    private Role name;
} 

@Entity
@Table(name = "student")
@Data
public class Student  extends User{
    @Id
    private Long id;
    private LocalDate dob;
    private String tell;
    private String course;

}

CodePudding user response:

In this link you can have an overview of the following approaches to inheritance:

  1. MappedSuperclass - the parent classes, can't be entities
  2. Single Table -The entities from different classes with a common ancestor are placed in a single table.
  3. Joined Table - Each class has its table, and querying a subclass entity requires joining the tables.
  4. Table per Class -All the properties of a class are in its table, so no join is required.

Regards

  • Related