lundi 22 mars 2021

Should I create a separate table in DB for UserAddress helper class Java, Hibernate, Postgres?

I am making a small restful service for user management. By assignment, the User is defined by the following values:

◦ First name 
◦ Last name 
◦ Date of birth 
◦ Login 
◦ Password 
◦ Input field “About me” 
◦ Address of residence (country, city, street, house, flat)

When designing, I paid attention to the address and thought that it would be wrong to write everything together in one address field and make a similar attribute in the database table, because then different filtering by addresses would become very inconvenient. Then I delimited the address field into 5 fields (those in brackets). However, having done so, I realized that my class, given the id field, has 13 fields, what, in my understanding, makes the class too overloaded and "wrong". Then I decided to make a separate class for the address and use it as a field for the user, namely:

@Entity
public class UserAddress {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private long id;
    private String country;
    private String city;
    private String street;
    private int house;
    private int flat;

}

And having done so, I'm not entirely sure how to proceed, as a result of which I have the following questions:

  1. Should I move 5 address fields into a separate class in UserAddres?
  2. If so, is it worth to make this class Entity and creating table in the database for it?
  3. Should I consider it like a complete class (create getters / setters, equals and hashcode, service layer)?
  4. Will filtering users by address become even more complicated than it was originally?
  5. What is the best way to deal with such a situation?

Aucun commentaire:

Enregistrer un commentaire