[英]How to decide if a separate entity and DAO is needed for an association table?
How to decide if a separate entity and DAO is needed for an association table? 如何确定关联表是否需要单独的实体和DAO?
Suppose I have 3 tables: user
, role
and user_role
. 假设我有3个表:
user
, role
和user_role
。 user_role
is the association table. user_role
是关联表。
create table user (user_id int not null primary key, login varchar(50) not null unique key, password varchar(100))
create table role (role_id int not null primary key, description varchar(100))
create table user_role (user_role_id int not null identity(1,1) primary key, user_id int not null foreign key references user(user_id), role_id int not null foreign key references role(role_id))
Now I will have separate entities and DAOs for user
and role
tables. 现在,我将为
user
和role
表使用单独的实体和DAO。
public class User {
private int userId;
private String login;
private String password;
// getters and setters
}
public interface UserDAO {
User getUserById(int userId);
void insertUser(User user);
void updateUserById(User user);
void deleteUserById(User user);
}
public class Role {
private int roleId;
private String description;
// getters and setters
}
public interface RoleDAO {
User getRoleById(int roleId);
void insertRole(Role role);
void updateRoleById(Role role);
void deleteRoleById(Role role);
}
I will have 3 screens: 我将有3个屏幕:
Screen 1: I will have a screen to maintain CRUD operations on user
table. 屏幕1:我将有一个屏幕来维护
user
表上的CRUD操作。
Screen 2: I will have a screen to maintain CRUD operations on role
table. 屏幕2:我将有一个屏幕来维护
role
表上的CRUD操作。
Screen 3: I will have a screen where users will be assigned to roles. 屏幕3:我将看到一个屏幕,将用户分配到角色。
But I am confused about how to tackle the user_role
table. 但是我对如何处理
user_role
表感到困惑。
a) Should User
entity have a list of associated Role
. a)
User
实体应具有关联Role
的列表。 And thats enough? 够了吗?
public class User {
private int userId;
private String login;
private String password;
private Set<Role> roles;
// getters and setters
}
b) Should Role
entity have a list of associated User
. b)
Role
实体应具有关联的User
列表。 And thats enough? 够了吗?
public class Role {
private int roleId;
private String description;
private Set<User> users;
// getters and setters
}
c) Should I have a separate entity and DAO for user_role
table like this: c)我应该为
user_role
表设置一个单独的实体和DAO,如下所示:
public class UserRole {
private int userRoleId;
private User user;
private Role role;
}
public interface UserRoleDAO {
User getUserRoleById(int userRoleId);
void insertUserRole(UserRole userRole);
void updateUserRoleById(UserRole userRole);
void deleteUserRoleById(UserRole userRole);
}
I guess as it a bidirectional relationship between user and roles you should have to have set of roles in User.java and set of users in Roles.java. 我猜因为它是用户和角色之间的双向关系,因此您必须在User.java中具有一组角色,在Roles.java中具有一组用户。 An separate entity user_roles is definitely required.
绝对需要单独的实体user_roles。 If you have dao for user you can have below methods in userdao
如果您有dao用户,可以在userdao中使用以下方法
User getUserRoleById(int userRoleId);
void insertUserRole(UserRole userRole);
void updateUserRoleById(UserRole userRole);
void deleteUserRoleById(UserRole userRole);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.