How to create classes for object / relational mapping?

0

I am new to hibernate and jpa, I have to create 3 classes User, Client and Attribute that will be mapped to tables, the classes have the following properties:

  • User has: id, name, surname, password, many clients, many Attributes.
  • Client has: id, name, user (client belongs to a user), many Attributes.
  • Attribute has id ?, name, value. ( example of a user attribute: Name: "Phone Number", Value: "333 222 111 44")
  • I do not have many problems creating the user and client classes, but when creating the Attribute class, I find myself somewhat confused, I do not know if I create 2 Attribute classes separated one for the user and another for the client or client. only 1.

    For now what I have done is this:

    Class User:

    public class User {
    
        private String userID;
        private String name;
        private String surname;
        private String password;
        private Set<Client> clients = new HashSet();
        private Set<UserAttribute> attributes = new HashSet();
    
        public User() {
    
        }
    
        /* getters and setters*/
    }
    

    Class Client:

    public class Client {
    
        private String clientID;
        private String clientName;
        private User user;
        private Set<ClientAttribute> attributes = new HashSet();
    
    
        public Client() {
    
        }
    
        /* getters and setters*/
    }
    

    Class UserAttribute:

    public class UserAttribute {
    
        private String id;
        private String name;
        private String value;
        private User user; // the owner of this attribute
    
        public UserAttribute() {
            // TODO Auto-generated constructor stub
        }
    
        /* getters and setters*/
    }
    

    Class ClientAttribute:

    public class ClientAttribute {
    
        private String id;
        private String name;
        private String value;
        private Client client; // the owner of this attribute
    
        public ClientAttribute() {
            // TODO Auto-generated constructor stub
        }
    
        /* getters and setters */
        }
    

    Is the way I write the classes okay? Should I use 2 classes or only 1 for Attribute?

        
    asked by Bryan Romero 07.11.2018 в 14:29
    source

    1 answer

    1

    First of all, design issues do not usually have a single answer and may depend on how you see the situation; for example, an option is to inherit but that can be complicated if someone decides that, in the end, the attributes of User and Client are going to be completely different.

    So:

  • Separate classes. Not very sophisticated, but it works, it's simple and flexible. It can be somewhat cumbersome if you end up having too many classes, and you have to write something else.

  • A class with property user and other client ; depending on which is related one or other of the attributes will have a value. It is a little ugly, and as you have to do the nullables attributes you have to take care in your logic that you do not have "orphan" lines that do not point to either user or% cliente .

  • Heritage:

    public class Attribute {
    
      private String id;
      private String name;
      private String value;
      // setters y getters
    }
    
    public class UserAttribute extends Attribute {
      private User user;
      // setters y getters
    }
    

    JPA allows different strategies on how these classes will be implemented in tables, it would be nice to have a look at "Inheritance in JPA". It's a pretty popular option, but you'll have problems if then someone decides that, for example, the UserAttribute have to have a field id but the ClientAttribute do not).

  • answered by 07.11.2018 / 20:36
    source