随笔-42  评论-578  文章-1  trackbacks-0

开发工具:Eclipse 3.4 for JavaEE

开发环境:JDK-6u14,JBoss5.0.1GA

从JPA开始,先设计四个类,分别为User, Role, Category, Article,它们之间的关系,如下图:

202

设置关联映射时,为简单起见,一个用户只能拥有一个角色,一篇文章只能属于一个栏目。而授权与权限管理方面,也为了简单起见,只设置角色拥有操纵哪些栏目的权限。

下面是我们的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 心梦帆影 阅读(1997) 评论(4)  编辑  收藏 所属分类: EJB3JPA

评论:
# re: EJB3.0开发用户授权与新闻发布系统(一) 2009-06-13 09:18 | metadmin
用户管理哦。如果设计到权限管理,尤其是数据级权限管理,欢迎参考 www.metadmin.com   回复  更多评论
  
# re: EJB3.0开发用户授权与新闻发布系统(一) 2009-06-13 11:00 | 小人物
学习了!  回复  更多评论
  
# re: EJB3.0开发用户授权与新闻发布系统(一) 2009-06-13 21:44 | metadmin
02年的时候,我线学习了EJB1.0,然后学习了2.0。 后来有hibernate这样的orm工具,抛弃了ejb。现在我有自己的ORM,也不用hibernate了。

http://metadmin.javaeye.com 这是我BLOG 讲了一些权限管理知识 欢迎来做客  回复  更多评论
  
# re: EJB3.0开发用户授权与新闻发布系统(一) 2009-06-19 20:34 | 吴丹勇
学习了!  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: