spring security使用数据库表管理账户及其角色(二)
前面使用了一个数据库表示了用户的信息及其角色信息。这次我要将角色表分离出来单独使用一张表来管理,由于用户和角色是多对多的关系,所以还需要一张用户和角色的关系表。数据库如下:
用户表:sec_user
@Entity@Table(name="SEC_ROLE")public class Role implements Serializable {/** * */private static final long serialVersionUID = 8216539800374390907L;@Id@Column(name="ROLENAME",unique=true,nullable=false,length=100)private String rolename;@Column(name="PROMPT",length=255)private String prompt;@ManyToMany(targetEntity=User.class,mappedBy="roles")private Collection<User> users;public String getRolename() {return rolename;}public void setRolename(String rolename) {this.rolename = rolename;}public String getPrompt() {return prompt;}public void setPrompt(String prompt) {this.prompt = prompt;}public Collection<User> getUsers() {return users;}public void setUsers(Collection<User> users) {this.users = users;}}添加实体类时,别忘了向hibernate.cfg.xml或applicationContext.xml中注册。
现在已经修改完成了。