skip to Main Content

I will make a Spring Boot project school management system. There are many types of users. These users can log in. Do I need to create a user entity and then extend other entities from the user class?

What makes me think here is that there is a relationship between entities.
Teacher, Student, Manager, Guardian

Since I am working on a single entity when creating the login system, I do not know how to do this through different entities.

@Data
@AllArgsConstructor
@RequiredArgsConstructor
@Builder
@Entity
@Table(name = "users")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private String id;
    private String username;
    private String password;
    private String name;
    private String surname;
    private String gender;
    private Role role;

}
@Entity
@Table(name = "teachers")
public class Teacher extends User{
    private String subject;
}
@Entity
@Table(name = "students")
public class Student extends User{

    private String illness;

    @ManyToOne
    @JoinColumn(name = "guardian_id")
    private Guardian guardian;
}


@Entity
@Table(name = "guardians")
public class Guardian extends User{
    @OneToMany(mappedBy = "guardian", cascade = CascadeType.ALL)
    private List<Student> students;
}

2

Answers


  1. I’m also looking for a way to perform an action through another entity. I’m curious.

    Login or Signup to reply.
  2. Let’s clarify some points regarding your design, especially considering the different user roles and relationships.

    Inheritance Strategy

    Using inheritance for your user types (e.g., Teacher, Student, Guardian, Manager) can be effective, but you need to choose an inheritance strategy that suits your needs. In JPA, you have several options:

    1. Single Table Inheritance: All entities are stored in a single table, with a discriminator column to differentiate between them. This can simplify queries and relationships but may lead to sparse tables if entities have very different fields.

    2. Joined Table Inheritance: Each entity is stored in its own table, but common fields are stored in a parent table. This approach can be cleaner and allows for more specific constraints per entity but may require more complex joins when querying.

    3. Table per Class: Each class is stored in a separate table with no shared columns. This can lead to redundancy and is typically not recommended unless you have very specific needs.

    Key Considerations

    1. Common Fields: Username, password, and general personal details like name, gender, etc. should be shared among all user types.
    2. Role Management: Each user should be assigned a role (Role) to handle their permissions and specific functionalities.
    3. Relationships: There are relationships between certain types of users, e.g., a Guardian is related to one or more Students.
    4. Database Design: A normalized approach using relationships and unique IDs per user type is ideal for managing different entities.
    5. Authentication/Authorization: Users should log in through a single unified system while maintaining their specific roles.

    Approach for different Entities

    • Specific User Types (Teacher, Student, Guardian)
    • Each specific user type (e.g., Teacher, Student, Guardian) will have its own entity class and will be linked to the User entity via a one-to-one relationship. These classes will contain additional fields specific to their roles.
    @Entity
    @Table(name = "teachers")
    public class Teacher {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
    
        private String subject;
    
        @OneToOne
        @JoinColumn(name = "user_id", referencedColumnName = "id")
        private User user;  // Maps to the common User entity
    }
    
    
    @Entity
    @Table(name = "students")
    public class Student {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
    
        private String illness;
    
        @OneToOne
        @JoinColumn(name = "user_id", referencedColumnName = "id")
        private User user;
    
        @ManyToOne
        @JoinColumn(name = "guardian_id")
        private Guardian guardian;  // Many students can have one guardian
    }
    
    
    @Entity
    @Table(name = "guardians")
    public class Guardian {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
    
        @OneToOne
        @JoinColumn(name = "user_id", referencedColumnName = "id")
        private User user;
    
        @OneToMany(mappedBy = "guardian", cascade = CascadeType.ALL)
        private List<Student> students;  // One guardian can have many students
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search