关系映射一对多,多对一
1, 一对多,如:部门表与用户表,一个部门是不是有多个用户,但是一个用户有且只有一个部门,很清晰了吧
2, 现在先写方部门类:Department.java
public class Deparment {
private int id;
private String name;
private Set<Employee> emps;
public Set<Employee> getEmps() {
return emps;
}
public void setEmps(Set<Employee> emps) {
this.emps = emps;
}
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;
}
}
3, 现在通过分析在写一个用户类:Employee.java
public class Employee {
private int id;
private String name;
//private int dpartId;根据员工查找到部门的id,在通过部门id查找部门的详细信息,操作麻烦,
//通过查找员工找到部门,对应部门的主键
private Deparment depart;
public Deparment getDepart() {
return depart;
}
public void setDepart(Deparment depart) {
this.depart = depart;
}
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;
}
}
4.映射文件
Department映射文件
<hibernate-mapping
package="com.hbsi.domain">
<class name="Deparment" table="deparment">
<id name="id"column="id">
<generator class="native"/>
</id>
<property name="name"/>
<set name="emps">
<key column="depart_id"/>
<one-to-many class="Employee"/>
</set>
</class>
</hibernate-mapping>
Employee映射文件
<hibernate-mapping
package="com.hbsi.domain">
<class name="Employee" table="employee">
<id name="id"column="id">
<generator class="native"/>
</id>
<property name="name"/>
<many-to-one name="depart"column="depart_id" not-null="true"></many-to-one>
</class>
</hibernate-mapping>
5.测试
多对一:
public class Manny2one {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generatedmethod stub
add();
queryq1(1);
//Employee e2=queryq1(3);懒加载会抛异常
//System.out.println(e2.getName());
//解决懒加载Hibernate.initialize(e.)
}
//
static Deparment add(){
Session s=null;
Transaction tx=null;
try{
s= HibernateUtil.getSession();
tx=s.beginTransaction();
//增加
Deparment dep=new Deparment();
dep.setName("hehe");
Employee e1=new Employee();
e1.setName("fdf");
//设置不能为空
e1.setDepart(dep);//对象模型的关联关系
s.save(dep);
s.save(e1);
//交换位置之后插入两次,更新一次,结果一样
//s.save(e1);
//s.save(dep);
tx.commit();
return dep;
}finally{
if(s!=null)
s.close();
}
}
//根据员工查询部门
static Employee queryq1(int empId){
Session s=null;
Transaction tx=null;
try{
s= HibernateUtil.getSession();
tx=s.beginTransaction();
//查询
Employee e=(Employee) s.get(Employee.class, empId);
System.out.println(e.getName()+e.getDepart().getName());
tx.commit();
return e;
}finally{
if(s!=null)
s.close();
}
}
}
一对多:
public class one2Many {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generatedmethod stub
add();
//queryq1(2);
}
static Deparment add(){
Session s=null;
Transaction tx=null;
try{
s= HibernateUtil.getSession();
tx=s.beginTransaction();
//增加
Deparment dep=new Deparment();
dep.setName("hehe");
Employee e1=new Employee();
e1.setName("fdf");
//设置不能为空
e1.setDepart(dep);//对象模型的关联关系告诉员工你在哪个部门
Employee e2=new Employee();
e2.setName("dong");
e2.setDepart(dep);//员工与部门建立关系
Set<Employee> set=new HashSet<Employee>();
set.add(e1);
set.add(e2);
dep.setEmps(set);//告诉部门你有哪些员工
s.save(dep);
s.save(e1);
s.save(e2);
//交换位置之后插入两次,更新一次,结果一样
//s.save(e1);
//s.save(dep);
tx.commit();
return dep;
}finally{
if(s!=null)
s.close();
}
}
static Deparmentqueryq1(int depId){
Session s=null;
Transaction tx=null;
try{
s= HibernateUtil.getSession();
tx=s.beginTransaction();
Deparment d=(Deparment) s.get(Deparment.class, depId);
System.out.println(d.getName()+"有"+d.getEmps().size()+"个员工");
Set<Employee> s2=d.getEmps();
for(Employee s3:s2){
System.out.println(s3.getName());
}
tx.commit();
return d;
}finally{
if(s!=null)
s.close();
}
}
}