Hibernate中表与表的关系模板代码
一对多的关系
多对一关系
多对多关系
需要第三表来维护
模板实例
实体类
Department.java
package cn.zzuli.oa.domain;
import java.util.HashSet;
import java.util.Set;
/**
* 部门
* @author LZH
* @date 2017年2月23日
*/
public class Department {
private Long id;
private Set users = new HashSet();
private Department parent;
private Set children = new HashSet();
private String name;
private String description;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Set getUsers() {
return users;
}
public void setUsers(Set users) {
this.users = users;
}
public Department getParent() {
return parent;
}
public void setParent(Department parent) {
this.parent = parent;
}
public Set getChildren() {
return children;
}
public void setChildren(Set children) {
this.children = children;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
Role.java
package cn.zzuli.oa.domain;
import java.util.HashSet;
import java.util.Set;
/**
* 岗位
* @author LZH
* @date 2017年2月23日
*/
public class Role {
private Long id;
private String name;
private String description;
private Set users = new HashSet();
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;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Set getUsers() {
return users;
}
public void setUsers(Set users) {
this.users = users;
}
}
User.java
package cn.zzuli.oa.domain;
import java.util.HashSet;
import java.util.Set;
/**
* 用户
* @author LZH
* @date 2017年2月23日
*/
public class User {
private Long id;
private Department department;
private Set roles = new HashSet();
private String loginName; // 登录名
private String password; // 密码
private String name; // 真实姓名
private String gender; // 性别
private String phoneNumber; // 电话号码
private String email; // 电子邮件
private String description; // 说明
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public Set getRoles() {
return roles;
}
public void setRoles(Set roles) {
this.roles = roles;
}
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;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
对应配置文件
Department.hbm.xml
Role.hbm.xml
User.hbm.xml
数据表模型
总结:
1、写注释
格式为:?属性,表达的是本对象与?的?关系。
例:“department属性,本对象与Department的多对一”
2、模版
多对一
一对多
多对多
3、填空
•name属性:属性名(注释中的第1问号)
•class属性:关联的实体类型(注释中的第2个问号)
•column属性:
•
•一对多中的
•多对多中的
•多对多中的