简体   繁体   English

我的数据库设计好吗?

[英]Is my database design is good?

I am building a market place application.I am designing the applications. 我正在构建市场应用程序。正在设计应用程序。 I have two three kind of users : 我有两种三种用户:

  1. admin : user who do administration tasks disable an account, add information to user, enable account, send message to user,... admin:执行管理任务的用户禁用帐户,向用户添加信息,启用帐户,向用户发送消息,...

  2. seller : user who create a product in the web site and sell it create product, edit product, deactivate product, change price 卖方:在网站上创建产品并销售该产品的用户,该用户创建产品,编辑产品,停用产品,更改价格

  3. buyer : user who buy product 买方:购买产品的用户

So it's user : A- has his own rights ( what is allowed to do ) B- has his own informations. 因此它是用户:A-拥有自己的权利(被允许做的事情)B-拥有自己的信息。 The seller will have a list of product while the buyer will have the list of transaction. 卖方将具有产品清单,而买方将具有交易清单。

I manage the user authentication and authorization using Spring Security Framework. 我使用Spring Security Framework管理用户身份验证和授权。 My application is developped in Java / Hibernate / Spring / AngularJS / Bootstrap. 我的应用程序是用Java / Hibernate / Spring / AngularJS / Bootstrap开发的。 I can manage rights with Spring security. 我可以使用Spring安全性管理权限。 This is not the problem. 这不是问题。

My question is about user information. 我的问题是关于用户信息。 Is it better to have an the following table : 拥有下表是否更好:

@Entity
@Table(name = "T_USER")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Inheritance(strategy=InheritanceType.JOINED)
public class User extends AbstractAuditingEntity implements Serializable {

    @NotNull
    @Size(min = 0, max = 50)
    @Id
    @Column(length = 50)
    private String login;

    @JsonIgnore
    @Size(min = 0, max = 100)
    @Column(length = 100)
    private String password;

    @Size(min = 0, max = 50)
    @Column(name = "first_name", length = 50)
    private String firstName;

    @Size(min = 0, max = 50)
    @Column(name = "last_name", length = 50)
    private String lastName;

    @Email
    @Size(min = 0, max = 100)
    @Column(length = 100)
    private String email;

//others attributes,getters and setters
}

The subclasses 子类

 @Entity
    @Table(name = "T_ADMIN")
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    public class Admin extends extends User{ 
// specific informations
}

 @Entity
    @Table(name = "T_SELLER")
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    public class Seller extends extends User{ 
// specific informations
}

 @Entity
    @Table(name = "T_BUYER")
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    public class Buyer extends User{ 
// specific informations
}

After the user is authenticate, i can use the following userServiceDetails to add information specific to the type of user : 在对用户进行身份验证之后,我可以使用以下userServiceDetails添加特定于用户类型的信息:

public class UserDetailServiceImpl implements UserDetailsService {
 private UserDAO userdao;

 public void setUserdao(UserDAO userdao) {
  this.userdao = userdao;
 }

 // this class is used by spring controller to authenticate and authorize
 // user
 @Override
 public UserDetails loadUserByUsername(String userId)
   throws UsernameNotFoundException {
  com.model.User u;
  try {
   u = userdao.get(userId);
   if (u == null)
    throw new UsernameNotFoundException("user name not found");

  } catch (DAOException e) {
   throw new UsernameNotFoundException("database error ");
  }
  return buildUserFromUserEntity(u);

 }

 private User buildUserFromUserEntity(com.model.User userEntity) {
  // convert model user to spring security user
  String username = userEntity.getUserId();
  String password = userEntity.getPassword();
  boolean enabled = true;
  boolean accountNonExpired = true;
  boolean credentialsNonExpired = true;
  boolean accountNonLocked = true;
  GrantedAuthority[] authorities = user.getAuthorities();
 User user = null;

if(authorities[0].getName.equals("ROLE_ADMIN"){
    user =  new Admin(username, password, enabled,
    accountNonExpired, credentialsNonExpired, accountNonLocked,
    authorities);
// set admin informations
}
else if(authorities[0].getName.equals("ROLE_SELLER"){
  user =  new Seller(username, password, enabled,
    accountNonExpired, credentialsNonExpired, accountNonLocked,
    authorities);
// set seller informations
} 
else if(authorities[0].getName.equals("ROLE_BUYER"){
  user =  new Buyer(username, password, enabled,
    accountNonExpired, credentialsNonExpired, accountNonLocked,
    authorities);
// set buyer informations
}


  return user;
 }

}

I am wondering is my database design about user hierarchy is good ? 我想知道我的关于用户层次结构的数据库设计好吗?

I would say go ahead with your implementation of it. 我会说继续执行它。

The only thing I can advise is that if you're doing queries with the SELLERS and BUYERS, the querys will be alot faster if you split them into different tables instead of one. 我唯一能建议的是,如果您正在使用卖方和买方进行查询,则将它们拆分为不同的表而不是一个表,查询会更快。

I don't know the specs of your application, so it may be perfectly correct, but your design don't lets a user to be a seller and a buyer at the same time. 我不知道您的应用程序的规格,因此它可能是完全正确的,但是您的设计不允许用户同时成为卖方和买方。

I think it will be more flexible if you only have one table for users and, on login, compose a list of roles for this user, so an user could have many roles, each one with their info and relationships. 我认为,如果您只为一个用户提供一个表,并在登录时为其组成一个角色列表,它将更加灵活,因此一个用户可以有多个角色,每个角色都有其信息和关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM