比如,我有一个User表,一个Project表,一个Role表。一个人可以在多个项目里面拥有多个角色。如何使用annotation来配置这样的关系?
[quote]人A,项目B,C,D,角色有E,F,G,这时候A在项目B上拥有角色E,在C上拥有F的角色。A在D项目也是角色E,你如何区分呢?你那样建立关系是没办法区分的。 [/quote]
那就是A拥有项目B,C,D
不明白你想表达的区分 是个什么概念
一个用户可以对应多个角色,一个角色可以对应多个项目
这样就可以达到你的一个一个人可以在多个项目里面拥有多个角色
[code="java"]/**
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
/**
@email suziwen1@gmail.com 2011-5-16
*/
@Entity
@Table(name = "k_user")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", length = 22)
private Long id;
@ManyToMany
@JoinTable(name = "k_user_role")
private Set roles = new HashSet();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Set getRoles() {
return roles;
}
public void setRoles(Set roles) {
this.roles = roles;
}
}[/code]
[code="java"]/**
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
/**
2011-5-16
*/
@Entity
@Table(name = "k_role")
public class Role implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", length = 22)
private Long id;
@ManyToMany(mappedBy = "roles")
@OrderBy("id asc")
private Set users = new HashSet();
@ManyToMany(mappedBy = "roles")
@OrderBy("id asc")
private Set projects = new HashSet();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Set getUsers() {
return users;
}
public void setUsers(Set users) {
this.users = users;
}
public Set getProjects() {
return projects;
}
public void setProjects(Set projects) {
this.projects = projects;
}
}[/code]
[code="java"]
/**
*/
package com.suziwen;
import java.io.Serializable;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
/**
2011-5-16
*/
@Entity
@Table(name = "k_project")
public class Project implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", length = 22)
private Long id;
@ManyToMany
@JoinTable(name="k_project_role")
@OrderBy("id asc")
private Set roles;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Set getRoles() {
return roles;
}
public void setRoles(Set roles) {
this.roles = roles;
}
}
[/code]
通过人找到角色,通过这些角色,找到这个人所对应的所有项目
[quote]人A,项目B,C,D,角色有E,F,G,这时候A在项目B上拥有角色E,在C上拥有F的角色。你如何区分呢?[/quote]
人A拥有角色E和F,E对应拥有的资源为项目B,F对应拥有的资源为项目C
如果你做项目的话,这不就和角色权限一样吗?