hibernate 关联映射 多对一

来源:互联网 发布:sql server导出excel 编辑:程序博客网 时间:2024/05/18 00:06
关联映射 多对一
 比如employee --- department
1 我们先创建一个employee类,其中要多一个depart 属性用于对应department

public class Employee {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Department getDepart() {
return depart;
}
public void setDepart(Department depart) {
this.depart = depart;
}
private String name;
private Department depart;
}


2 创建一个 department类
public class Department {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private String name;
}




3 接着创建employee.hbm.xml ,要加上many-to-one 这个属性来表示对应关系
<hibernate-mapping>


<class name="Employee">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<many-to-one name="depart" column="depart_id" />
</class>
</hibernate-mapping>
0 0
原创粉丝点击