用户权限关系
涉及三个数据库表
SYS_USER,SYS_USER_ROLE,SYS_ROLE
用户类
package pineapple.web.pal.model;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
@Entity
@Table(name="SYS_USER")
public class User implements Serializable{
@Id
@SequenceGenerator(name = "SEQ_USER", sequenceName = "SEQ_USER")
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "SEQ_USER")
private Integer id;
@Column(name = "USERNAME", length = 100, nullable = false)
private String username;
@Column(name = "FULLNAME", length = 255)
private String fullname;
@Column(name = "PASSWORD", nullable = false)
private String password;
@Column(name = "ENABLED")
private Integer enabled;
@Column(name = "IS_ADMIN")
private Integer isAdmin;
@Column(name = "VERSION")
private Integer version;
@Cascade(value={CascadeType.SAVE_UPDATE})
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(schema = "TRAIN", name = "SYS_USER_ROLE", joinColumns = { @JoinColumn(name = "USER_ID", referencedColumnName = "ID", nullable = false) }, inverseJoinColumns = { @JoinColumn(name = "ROLE_ID", referencedColumnName = "ID", nullable = false) })
Set<Role> roles;
权限类
package pineapple.web.pal.model;
import java.io.Serializable;
import java.util.Set;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
@Entity
@Table(schema = "TRAIN", name = "SYS_ROLE")
public class Role implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name = "SEQ_ROLE", sequenceName = "SEQ_ROLE")
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "SEQ_ROLE")
Integer id;
@Column(name = "NAME", length = 50, nullable = false)
@Basic(fetch = FetchType.EAGER)
String name;
@Column(name = "DESCRIPTION")
@Basic(fetch = FetchType.EAGER)
String description;
@Column(name = "ENABLED")
@Basic(fetch = FetchType.EAGER)
Integer enabled;
@Column(name = "IS_ADMIN")
@Basic(fetch = FetchType.EAGER)
Integer isAdmin;
@Column(name = "VERSION")
@Basic(fetch = FetchType.EAGER)
Integer version;
@Cascade(value={CascadeType.SAVE_UPDATE})
@ManyToMany(mappedBy = "roles", fetch = FetchType.LAZY)
java.util.Set<User> users;
这里涉及到JPA的annotation跟Hibernate的annotation,其中基本都是用的JPA的标准标注,但是@Cascade(value={CascadeType.SAVE_UPDATE})这一句的cascade用的是Hibernate的,因为本例子是在spring+Hibernate环境下实现的,并没有用到JPA,所以如果在@ManyToMany标注下声明@Cascade(value={CascadeType.persist})并不能生效。