开发工具:Eclipse 3.4 for JavaEE
开发环境:JDK-6u14,JBoss5.0.1GA
从JPA开始,先设计四个类,分别为User, Role, Category, Article,它们之间的关系,如下图:
设置关联映射时,为简单起见,一个用户只能拥有一个角色,一篇文章只能属于一个栏目。而授权与权限管理方面,也为了简单起见,只设置角色拥有操纵哪些栏目的权限。
下面是我们的4个类的代码:
用户类(User.java)
package rong.entity;
import java.io.Serializable;
import javax.persistence.CascadeType;
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.ManyToOne;
/** *//**
* 用户
* @author rongxinhua
*/
@Entity //JPA注解,声明为实体类
public class User implements Serializable {
private Long id; //ID
private String loginName; //登录名
private String password; //登录密码
private Role role; //用户角色
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@ManyToOne(cascade = {CascadeType.MERGE, CascadeType.PERSIST}, fetch = FetchType.LAZY)
@JoinColumn(name = "role_id")
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
}
角色类(Role.java)
package rong.entity;
import java.io.Serializable;
import java.util.LinkedHashSet;
import java.util.Set;
import javax.persistence.CascadeType;
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.OneToMany;
/** *//**
* 角色
* @author rongxinhua
*/
@Entity //JPA注解,声明为实体类
public class Role implements Serializable{
private Long id; //角色ID
private String name; //角色名称
private Set<User> users = new LinkedHashSet<User>(); //用户集合
private Set<Category> categorys = new LinkedHashSet<Category>(); //能操纵的栏目集合
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToMany(cascade = {CascadeType.ALL}, fetch = FetchType.LAZY, mappedBy = "role")
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.LAZY, mappedBy = "roles")
public Set<Category> getCategorys() {
return categorys;
}
public void setCategorys(Set<Category> categorys) {
this.categorys = categorys;
}
}
栏目类(Category.java)
package rong.entity;
import java.io.Serializable;
import java.util.LinkedHashSet;
import java.util.Set;
import javax.persistence.CascadeType;
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.OneToMany;
/** *//**
* 栏目.
* @author rongxinhua
*/
@Entity //JPA注解声明为实体类
public class Category implements Serializable{
private Long id;
private String name;
private Set<Role> roles = new LinkedHashSet<Role>();
private Set<Article> articles = new LinkedHashSet<Article>();
@Id @GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE},fetch = FetchType.LAZY)
@JoinTable(
name = "category_role",
joinColumns = {@JoinColumn(name = "category_id")},
inverseJoinColumns = {@JoinColumn(name = "role_id")}
)
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
@OneToMany(cascade = {CascadeType.ALL}, fetch = FetchType.LAZY, mappedBy = "category")
public Set<Article> getArticles() {
return articles;
}
public void setArticles(Set<Article> articles) {
this.articles = articles;
}
}
文章类(Article.java)
package rong.entity;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.CascadeType;
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.Lob;
import javax.persistence.ManyToOne;
/** *//**
* 文章
* @author rongxinhua
*/
@Entity //声明为实体类
public class Article implements Serializable{
private Long id;
private String title;
private String author;
private String content;
private Date pubtime;
private Category category;
@Id @GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Lob
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Date getPubtime() {
return pubtime;
}
public void setPubtime(Date pubtime) {
this.pubtime = pubtime;
}
@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.LAZY)
@JoinColumn(name = "category_id")
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
}
本文原创,转载请注明出处,谢谢!http://www.blogjava.net/rongxh7(心梦帆影JavaEE技术博客)
posted on 2009-06-13 02:15
心梦帆影 阅读(2007)
评论(4) 编辑 收藏 所属分类:
EJB3 、
JPA