1.多个Person对应一个department的情况
person类:
public class Person {
private String pno;
private String name;
private String address;
private int age;
private Department dept;
public Department getDept() {
return dept;
}
public void setDept(Department dept) {
this.dept = dept;
}
public String getPno() {
return pno;
}
public void setPno(String pno) {
this.pno = pno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
person类对应的person.hbm.xml的配置
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping >
<class name="com.bruce.entity.Person" table="t_mtoo_person" >
<id name="pno"> <!-- uuid是hibernate自己生成的唯一的标示 -->
<generator class="uuid"/>
</id>
<property name="name"/>
<property name="address"/>
<property name="age"/>
<!-- 配置多对一的关系,dept是 ”多“中的”一“的实体属性 -->
<many-to-one name="dept" column="dept1" >
</many-to-one>
</class>
</hibernate-mapping>
department 类:
private String deptno;
private String deptname;
private String deptaddress;
public String getDeptno() {
return deptno;
}
public void setDeptno(String deptno) {
this.deptno = deptno;
}
public String getDeptname() {
return deptname;
}
public void setDeptname(String deptname) {
this.deptname = deptname;
}
public String getDeptaddress() {
return deptaddress;
}
public void setDeptaddress(String deptaddress) {
this.deptaddress = deptaddress;
}
department.hbm.xml的配置 :
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping >
<class name="com.bruce.entity.Department" table="t_mtoo_department" >
<id name="deptno">
<generator class="uuid"/>
</id>
<property name="deptname"/>
<property name="deptaddress"/>
</class>
</hibernate-mapping>
然后就是在 Junit中对其进行测试,首先添加表:
public void createTable(){
Configuration cfg = new Configuration().configure();//读hibernate配置文件
SchemaExport se = new SchemaExport(cfg);//获取创建表的对象
se.create(true, true);//创建表
}
然后在插入数据:
public void AddData(){
Configuration cfg = new Configuration().configure();//读取Hibernate配置文件
SessionFactory sessionfactory = cfg.buildSessionFactory();//获取会话工厂
Session session = sessionfactory.openSession();//打开会话工厂(获取会话)
Transaction ts = session.beginTransaction();//开启事务
Department dept = new Department();
dept.setDeptname("计生局");
dept.setDeptaddress("眉山");
Person person = new Person();//创建对象
person.setName("niam");
person.setAddress("asdfasdf");
person.setAge(12);
person.setDept(dept);
Person person1 = new Person();//创建对象
person1.setName("niam111");
person1.setAddress("asdfasdf111");
person1.setAge(12);
person1.setDept(dept);
/*
* 如果在<many-to-one name="dept" column="dept1">
</many-to-one>中配置了 cascade="save-update"的话此处就
不必执行 session.save(dept);(保存部门先保证多对一中的一),否则就必须
先保存 多对一中的一 */
session.save(dept);
session.save(person1);
session.save(person);//将对象存到会话中
ts.commit();//手动提交事务
session.close();
}
在介绍多对多的关系,其实也很简单,只需要在deparment的实体中添加一个 Set persons属性 eg:
private Set persons;
public Set getPersons() {
return persons;
}
public void setPersons(Set persons) {
this.persons = persons;
}
然后在配置文件中加上 :
<set name="persons">
<key column="deptId"></key>
<one-to-many class="com.bruce.entity.Person"/>
</set>
然后就是在测试类中对其进行迭代输出:
(获取的时候都是经过Id 很纠结的一串,就不写了)
for(Object p:dept.getPersons().toArray()){
Person p1 = (Person)p;
System.out.println("p.name:"+p1.getName());
}