How to handle inheritance in Java Spring boot (Gradle)

216 views Asked by At

Well I'm quite new to this area and trying to understand the concepts of inheritance relationship mapping in hibernate .

The scenario I'm facing is to build a recruitment web app which have different users , such as:

  1. ForeignCompany
  2. LocalCompany
  3. JobSeeker

Thereby user class will be the super-class and I will be using @MappedSuperclass for mapping the relationships of inheritance.

I have done the mapping and now where I'm stuck with is how to make repositories and make calls such as :

i. RegisterUser() : because the User class is super class it cant be instantiated , the biggest issue I'm facing is that I'm doing it right or what would be the best practice to follow ? maybe the issue is I'm new and doesn't get it . Your help would mean a lot thanks .

The code :

@Data
@NoArgsConstructor
@AllArgsConstructor
@MappedSuperclass
public class User {

    // Defining member fields
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long userId;

    @Enumerated(EnumType.STRING)
    private Role role;
    private Date userDob;
    private String userAddress;
    private String username;
    private String userMail;
    private String userPassword;


}

@Data
@Entity
@EqualsAndHashCode(callSuper=false)
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class ForeignCompany extends User{

    //Defining member fields
    private String companyName;


}

Service class :

Is this the right way to save the password ?

@Service public class UserServiceImpl implements UserService{

//Create member functions

@Override
public User saveUser(User user){
   user.setUserPassword(passwordEncoder.encode(user.getUserPassword()));
    return userRepository.save(user);
}

service interface:

public interface UserService {
    User saveUser(User user);
}
0

There are 0 answers