使用Hibernate逆向工程,将数据库中的四张表逆向成 对象 和 映射表*.hbm.xml
Role.java
public class Role implements java.io.Serializable {
// Fields
private Integer roleId;
private String rolename;
private String roledesc;
private Set userInfos = new HashSet(0);
private Set rolerights = new HashSet(0);
// Constructors
/** default constructor */
public Role() {
}
/** full constructor */
public Role(String rolename, String roledesc, Set userInfos, Set rolerights) {
this.rolename = rolename;
this.roledesc = roledesc;
this.userInfos = userInfos;
this.rolerights = rolerights;
}
// Property accessors
public Integer getRoleId() {
return this.roleId;
}
public void setRoleId(Integer roleId) {
this.roleId = roleId;
}
public String getRolename() {
return this.rolename;
}
public void setRolename(String rolename) {
this.rolename = rolename;
}
public String getRoledesc() {
return this.roledesc;
}
public void setRoledesc(String roledesc) {
this.roledesc = roledesc;
}
public Set getUserInfos() {
return this.userInfos;
}
public void setUserInfos(Set userInfos) {
this.userInfos = userInfos;
}
public Set getRolerights() {
return this.rolerights;
}
public void setRolerights(Set rolerights) {
this.rolerights = rolerights;
}
}
role与rights是多对多的关系。所以在role.hbm.xml中设置<Set>标签
<!-- role rights多对多 -->
<set name="rolerights" table="roleright" cascade="all" lazy="false" inverse="true">
<key>
<column name="roleId" not-null="true" />
</key>
<many-to-many entity-name="com.po.Roleright">
<column name="rightId" not-null="true"/>
</many-to-many>
</set>
role与user是一对多的关系。所以在role.hbm.xml中设置<Set>标签
<set name="userInfos" table="userinfo" inverse="true" lazy="false" fetch="select" cascade="delete">
<key>
<column name="roleId" not-null="true"/>
</key>
<one-to-many class="com.po.UserInfo" />
</set>
Rights.java
由于rights表中有一个字段是自己引用自己的id,所以在Rights.jsp中修改属性。
public class Rights implements java.io.Serializable {
// Fields
private Integer rightId;
private Rights rightParent;
private String rightname;
private String url;
private String rightdesc;
//引用自己的表而建的字段
private Set<Rights> children = new HashSet<Rights>(0);
//与role
private Set<Role> roles = new HashSet<Role>(0);
// Constructors
/** default constructor */
public Rights() {
}
/** minimal constructor */
public Rights(String rightname) {
this.rightname = rightname;
}
/** full constructor */
public Rights(Rights rightParent, String rightname, String url,
String rightdesc, Set<Rights> children, Set<Role> roles) {
this.rightParent = rightParent;
this.rightname = rightname;
this.url = url;
this.rightdesc = rightdesc;
this.children = children;
this.roles = roles;
}
// Property accessors
public Integer getRightId() {
return this.rightId;
}
public void setRightId(Integer rightId) {
this.rightId = rightId;
}
public Rights getRightParent() {
return rightParent;
}
public void setRightParent(Rights rightParent) {
this.rightParent = rightParent;
}
public String getRightname() {
return this.rightname;
}
public void setRightname(String rightname) {
this.rightname = rightname;
}
public String getUrl() {
return this.url;
}
public void setUrl(String url) {
this.url = url;
}
public String getRightdesc() {
return this.rightdesc;
}
public void setRightdesc(String rightdesc) {
this.rightdesc = rightdesc;
}
public Set<Rights> getChildren() {
return children;
}
public void setChildren(Set<Rights> children) {
this.children = children;
}
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
}
Rights.hbm.xml
自己引用自己的字段,即‘父类’
<many-to-one name="rightParent" class="com.po.Rights" fetch="select">
<column name="parentId" />
</many-to-one>
<!-- 与role -->
<set name="roles" table="roleright" inverse="true" lazy="false" cascade="all">
<key>
<column name="rightId" not-null="true" />
</key>
<many-to-many class="com.po.Role">
<column name="roleId" not-null="true"/>
</many-to-many>
</set>
<!-- 与children -->
<set name="children" inverse="true" lazy="false" cascade="all">
<key>
<column name="parentId" />
</key>
<one-to-many class="com.po.Rights" />
</set>
UserInfo.java
public class UserInfo implements java.io.Serializable {
// Fields
private Integer userId;
private String userName;
private String password;
private String realName;
private String sex;
private Role role;
// Constructors
/** default constructor */
public UserInfo() {
}
/** minimal constructor */
public UserInfo(String userName, String password) {
this.userName = userName;
this.password = password;
}
/** full constructor */
public UserInfo(Role role, String userName, String password, String realName, String sex) {
this.role = role;
this.userName = userName;
this.password = password;
this.realName = realName;
this.sex = sex;
}
// Property accessors
public Integer getUserId() {
return this.userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public Role getRole() {
return this.role;
}
public void setRole(Role role) {
this.role = role;
}
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRealName() {
return this.realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
public String getSex() {
return this.sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
一个用户拥有一个角色,一个角色包含很多用户
<many-to-one name="role" class="com.po.Role" fetch="join" lazy="false">
<column name="roleId" not-null="false"/>
</many-to-one>