Hibernate 一对多映射
- java类
public class Department{
private String departmentId;
private String departmentName;
private List<Employee> employee;
public Department() {
}
public Department(String departmentId) {
this.departmentId = departmentId;
}
public Department(String departmentId, String departmentName,List<Employee> personList) {
this.departmentId = departmentId;
this.departmentName = departmentName;
this.employee=personList;
}
public String getDepartmentId() {
return this.departmentId;
}
public void setDepartmentId(String departmentId) {
this.departmentId = departmentId;
}
public String getDepartmentName() {
return this.departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
public List<Employee> getEmployee() {
return employee;
}
public void setEmployee(List<Employee> employee) {
this.employee = employee;
}
}
public class Employee{
private String id;
private String name;
private String sex;
private Department department;
private IdentityCard identityCard;
public Employee() {
}
public Employee(String id) {
this.id = id;
}
public Employee(String id, String name, String sex ){
this.id = id;
this.name = name;
this.sex = sex;
}
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return this.sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Department getDepartment() {
return this.department;
}
public void setDepartment(Department department) {
this.department = department;
}
public IdentityCard getIdentityCard() {
return identityCard;
}
public void setIdentityCard(IdentityCard identityCard) {
this.identityCard = identityCard;
}
}
- 映射文件
<hibernate-mapping package="com.woo.demo.hibernate.domain">
<class name="Employee" table="EMPLOYEE">
<id name="id" type="java.lang.String">
<column name="ID" />
<generator class="uuid"/>
</id>
<property name="name" type="text">
<column name="NAME" />
</property>
<property name="sex" type="string">
<column name="SEX" />
</property>
<many-to-one name="department" class="Department" >
<column name="DEPARTMENT_ID"/>
</many-to-one>
</class>
</hibernate-mapping>
<hibernate-mapping package="com.woo.demo.hibernate.domain">
<class name="Department" table="DEPARTMENT">
<id name="departmentId" type="java.lang.String">
<column name="DEPARTMENT_ID" />
<generator class="uuid" />
</id>
<property name="departmentName" type="java.lang.String">
<column name="DEPARTMENT_NAME" />
</property>
<bag name="employee" cascade="save-update">
<key column="DEPARTMENT_ID"/>
<one-to-many class="Employee"/>
</bag>
</class>
</hibernate-mapping>
- 关系数据库结构