隨筆-42  評論-578  文章-1  trackbacks-0

          開發(fā)工具:Eclipse 3.4 for JavaEE

          開發(fā)環(huán)境: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;
              }


          }



          本文原創(chuàng),轉載請注明出處,謝謝!http://www.aygfsteel.com/rongxh7(心夢帆影JavaEE技術博客)
              

          posted on 2009-06-13 02:15 心夢帆影 閱讀(2019) 評論(4)  編輯  收藏 所屬分類: EJB3JPA

          評論:
          # re: EJB3.0開發(fā)用戶授權與新聞發(fā)布系統(tǒng)(一) 2009-06-13 09:18 | metadmin
          用戶管理哦。如果設計到權限管理,尤其是數據級權限管理,歡迎參考 www.metadmin.com   回復  更多評論
            
          # re: EJB3.0開發(fā)用戶授權與新聞發(fā)布系統(tǒng)(一) 2009-06-13 11:00 | 小人物
          學習了!  回復  更多評論
            
          # re: EJB3.0開發(fā)用戶授權與新聞發(fā)布系統(tǒng)(一) 2009-06-13 21:44 | metadmin
          02年的時候,我線學習了EJB1.0,然后學習了2.0。 后來有hibernate這樣的orm工具,拋棄了ejb。現在我有自己的ORM,也不用hibernate了。

          http://metadmin.javaeye.com 這是我BLOG 講了一些權限管理知識 歡迎來做客  回復  更多評論
            
          # re: EJB3.0開發(fā)用戶授權與新聞發(fā)布系統(tǒng)(一) 2009-06-19 20:34 | 吳丹勇
          學習了!  回復  更多評論
            
          主站蜘蛛池模板: 洱源县| 开封县| 丁青县| 青河县| 宜春市| 乌拉特中旗| 海兴县| 潞西市| 资阳市| 新平| 广西| 乌苏市| 凤山市| 高邮市| 绥芬河市| 乌拉特后旗| 杂多县| 望谟县| 仪征市| 马关县| 龙井市| 阿克苏市| 武隆县| 张家口市| 体育| 九江县| 洛隆县| 蓝田县| 图片| 乳源| 闽侯县| 吴川市| 扎囊县| 宣汉县| 延边| 奇台县| 纳雍县| 怀化市| 灵宝市| 涟源市| 南丰县|