0

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);
}
Mehdi Rahimi
  • 1,453
  • 5
  • 20
  • 31
Bryan
  • 63
  • 1
  • 7
  • 1
    This goes probably far beyond the scope of your question, but from a security point of view you should _never_ handle plain text passwords in a `String`. You have no control, when garbage collection will actually delete it. Better to put it into a `char[]` before you encode it, and after that, clear out each entry of that array asap. – cyberbrain Apr 21 '22 at 18:08
  • 1
    You need to handle your subclasses as individual entities in the database related view. Use the super class only for internal stuff that is not directly database related, like business logic or utility methods. – cyberbrain Apr 21 '22 at 18:13
  • @cyberbrain thanks i will keep em in mind –  Bryan Apr 21 '22 at 19:47

0 Answers0