新建部门类
private int deptId;
private String deptName;
private Set emps = new HashSet(); //部门包含员工集合
新建员工类
private int empId;
private String empName;
private int empAge;
private Dept dept; //部门对象
新建Dept.hbm.xml
<class name="com.tfsoft.two.Dept">
<id name="deptId">
<generator></generator>
</id>
<property name="deptName" length="10"></property>
<!-- 一对多 -->
<set name="emps" inverse="true"><!-- 集合中使用,表示由对方维护主外键关系 -->
<key column="deptFk" not-null="true"></key>
<one-to-many class="com.tfsoft.two.Employee"/>
</set>
</class>
inverse=“false”是默认的值。
inverse只对集合起作用,也就是只对one-to-many或many-to-many有效(因 为只有这两种关联关系包含集合,而one-to-one和many-to-one只含有关系对方的一个引用)。
<one-to-many>中,建议inverse=”true”,由“many”方来进行关联关系的维护
<many-to-many>中,只设置其中一方inverse=”false”,或双方都不设置
新建Employee.hbm.xml
<class name="com.tfsoft.two.Employee">
<id name="empId">
<generator></generator>
</id>
<property name="empName" length="10"></property>
<property name="empAge"></property>
<!-- 多对一 -->
<!-- cascade默认属性,unsaved-value默认为Null -->
<many-to-one name="dept" class="com.tfsoft.two.Dept" lazy="false">
<column name="deptFk" not-null="true"></column><!-- 映射到数据库中的名称和设置 -->
</many-to-one>
</class>
新建测试类
public class Two {
private static Configuration cf = null;
private static SessionFactory sf = null;
static{
cf = new Configuration().configure();
sf = cf.buildSessionFactory();
}
//新增部门
public void saveOrUpdateDept(Dept dept){
Session session = sf.openSession();
Transaction ts = session.beginTransaction();
session.saveOrUpdate(dept);
ts.commit();
session.close();
}
//通过部门主键获取对象
public Dept getDeptById(int deptId){
Session session = sf.openSession();
Dept dept = (Dept)session.get(Dept.class, deptId);
session.close();
return dept;
}
//新增员工
public void saveOrUpdateEmp(Employee emp){
Session session = sf.openSession();
Transaction ts = session.beginTransaction();
session.saveOrUpdate(emp);
ts.commit();
session.close();
}
//通过员工主键获取对象
public Employee getEmpById(int empId){
Session session = sf.openSession();
Employee emp = (Employee)session.get(Employee.class, empId);
session.close();
return emp;
}
public static void main(String[] args) {
Two two = new Two();
/* Dept dept = new Dept(); 新增部门
dept.setDeptName("人事部");
two.saveOrUpdateDept(dept);*/
/* Employee emp = new Employee(); 新增员工,同时获取部门,添加到部门里面
emp.setEmpAge(20);
emp.setEmpName("张三");
Dept dept = two.getDeptById(1);
emp.setDept(dept);
two.saveOrUpdateEmp(emp);*/
/* Employee emp = two.getEmpById(1); 通过Id获取员工
System.out.println(emp.getEmpId()+"::"+emp.getEmpName());
Dept dept = emp.getDept(); 获取员工所在的部门
System.out.println(dept.getDeptId());
System.out.println(dept.getDeptName());*/ lazy="false"时,可以获取部门的名称,为true只能获取id
Dept dept = two.getDeptById(1); 通过Id 获取部门
System.out.println(dept.getDeptName());
Set emps = dept.getEmps(); 通过部门查询员工集合
Iterator it = emps.iterator(); 迭代器循环打印
while(it.hasNext()){
Employee emp = (Employee)it.next();
System.out.println(emp.getEmpName());
}
}
}
一对一
新建身份证类
private int CarId;
private String CarNum;
private Person person;
新建人类
private int perId;
private String perName;
新建Card.hbm.xml文件
<class name="com.hou.onetoone.Card" table="card">
<id name="carId" column="carId">
<generator></generator>
</id>
<property name="carNum" column="carNum" type="string" length="18"></property>
<one-to-one name="person" class="com.hou.onetoone.Person"
constrained="true">
</one-to-one>
</class>
constrained只能在one-to-one的映射中使用,(一般在主表的映射中,有外键的那个表)。如果constrained=true,则表明存在外键与关联表对应,并且关联表中肯定存在对应的键与其对应, 另外该选项最关键的是影响save和delete的先后顺序。例如增加的时候,如果constainted=true,则会先增加关联表,然后增加本表。删除的时候反之
one-to-one的单向关联中,如果constrained=false,则会在查询时就全部取出来,用left outer join的方式。如果constrained=true,hibernate即会延迟加载sql,只把本表的查出来,等有用到关联表的再发sql取。
one-to-one的双向关联中,必须设置constrained=true,要不然会有重复数据。
新建Person.hbm.xml文件
<class name="com.hou.onetoone.Card" table="card">
<id name="carId" column="carId">
<generator></generator>
</id>
<property name="carNum" column="carNum" type="string" length="18"></property>
<one-to-one name="person" class="com.hou.onetoone.Person"
constrained="true">
</one-to-one>
</class>
新建测试类
public class Onetoone {
private static Configuration cf = null;
private static SessionFactory sf = null;
static{
cf = new Configuration().configure();
sf = cf.buildSessionFactory();
}
//新增人
public void saveOrUpdatePerson(Person per){
Session session=sf.openSession();
Transaction ts=session.beginTransaction();
session.saveOrUpdate(per);
ts.commit();
session.close();
}
//新增身份证
public void saveOrUpdateCard(Card car){
Session session=sf.openSession();
Transaction ts=session.beginTransaction();
session.saveOrUpdate(car);
ts.commit();
session.close();
}
//通过Id获取人
public Person getPerById(int perId){
Session session=sf.openSession();
Transaction ts=session.beginTransaction();
Person per=(Person)session.get(Person.class, perId);
ts.commit();
session.close();
return per;
//通过Id获取身份证
}public Card getCarById(int CarId){
Session session=sf.openSession();
Transaction ts=session.beginTransaction();
Card car=(Card)session.get(Card.class, CarId);
ts.commit();
session.close();
return car;
}
public static void main(String[] args) {
Onetoone oto=new Onetoone();
Person per=new Person();/* 新增人
per.setPerName("侯强强");
oto.saveOrUpdatePerson(per);*/
/* Card car=new Card(); 获取人添加进身份证
car.setCarNum("12345678912");
car.setPerson(oto.getPerById(1));
oto.saveOrUpdateCard(car);*/
/* Card car=oto.getCarById(1); 获取Id
car.setCarNum("111"); 修改身份证号码
car.setCarId(1);
oto.saveOrUpdateCard(car);*/
Card car=oto.getCarById(1); 通过Id 获取对象
System.out.println(car.getPerson().getPerName()); 打印身份证对应的人的姓名,添加lazy=”false”
Constrained=”false”时,数据库外键不显示,查询时通过左连接查询出姓名
}
}