1 2 3 4 下一页 开发工具:Eclipse 3.4 for JavaEE 开发环境:JDK-6u14,JBoss5.0.1GA 从JPA开始,先设计四个类,分别为User, Role, Category, Article,它们之间的关系,如下图: 设置关联映射时,为简单起见,一个用户只能拥有一个角色,一篇文章只能属于一个栏目。而授权与权限管理方面,也为了简单起见,只设置角色拥有操纵哪些栏目的权限。 下面是我们的4个类的代码: 用户类(User.java) package rong.entity;
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 { 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; }
}
(责任编辑:admin) |