samedi 10 novembre 2018

How to handle one to many relations with DAO?

Suppose I have two database tables :

USER (id int primary key, login varchar(20), password varchar(20), role int not null)
ROLE (id int primary key, name varchar(20))

Then I create two entities and DAOs for them :

public class User {
  private int id;
  private String login;
  private String password;
  private Role role;
  //getters and setters
}
public interface UserDAO {
  User getById(int id);
  void insert(User user);
  void update(User user);
  void delete(User user);
}

public class Role {
  private int id;
  private String name;
  // getters and setters
}
public interface RoleDAO {
  User getById(int id);
  void insert(Role role);
  void update(Role role);
  void delete(Role role);
}

How then I have to handle selection of user if it only returns only the role's id? Can I create a constructor in Role that takes the id and then uses RoleDAO to retrieve the role's name?

Aucun commentaire:

Enregistrer un commentaire