表与表之间关系
--------实体与实体之间关系
我们知道的表与表之间有三种关系1.一对一 2.多对一 3.多对多
首先我们来讲一下第二种多对一的关系:其中有多对一,和一对多。
我们用一个实例来说明:
Department和Employee 这个我们知道是一个一对多的关系:
我们说一个简单点的 在department中有两个属性 id 和name
在employee中有三个字段 id、name和Deployment 的depart
我们首先说一下Emplyee对象中的第三个字段。我们为什么要这样写呢?为什么外间要这样写呢?我们知道如果定义成一个int类型的也能用!但是呢?我们只能得到int这一个字段的类型,但是如果我们要想得到其他的如:emplyee中的name呢?不是得不到而是很麻烦?
所以我们用一个类定义一个属性,这样我们得到里面的内容我们一起直接用get方法得到!原因只在于此
这是多对一
我们还是看看Department这个类是怎么写的吧!
public classDepartment {
private int id;
private String name;
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;
}
}
其实就是get和set方法。
我们再来看一下这个的xml文件
<hibernate-mapping package="domain"><!-- 包明子就是实体类在那个包里面 -->
<class name="Department" table="department">
<id name="id"column="id">
<generator class="native"/>
</id>
<property name="name" not-null="true"/>
</class>
</hibernate-mapping>
因为这个类中没有所谓的外键,所以呢?我们没有特殊的地方
我们再来看看这个employee这个类是怎么写的把!
public classEmployee {
private int id;
private String name;
private Department depart;
public Department getDepart(){
return depart;
}
public void setDepart(Departmentdepart) {
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;
}
}
其实我们看清楚点,也就是get和set方法!但是里面多了一个Deployment类型的对象。
<hibernate-mapping package="domain">
<class name="Employee" table="employee">
<id name="id"column="id">
<generator class="native"/>
</id>
<property name="name" not-null="true"/>
<many-to-one name="depart" column="depart_id"/>
</class>
</hibernate-mapping>
看看这个类,我们清楚了我们在其中看到了一个特别的地方也就是那个<many-to-one>这个标签
我们来解释一下!其中的name就是我们在Employee中定义的一个类型是Deployment我们后面的column就是对应的表中的外间的字段名字。
我们在我们的射影文件里面配置一下ok了。
我们在设计一个测试的类:
Session session=null;
Transaction tx=null;
try{
session=hibernateUtil.getSession();
tx=session.beginTransaction();
Department department=newDepartment();
department.setName("depart name");
Employee employee=newEmployee();
employee.setName("emp name");
employee.setDepart(department);//建立对象模型,让两个对象关联
|
session.save(department);
session.save(employee);
tx.commit();
return department;
}finally{
if (session!=null) {
session.close();
}
}
}
其中的方法是add();我们很清楚了基本的操作。我们从构造department对象开始吧!首先,我们构建一个department的对象然后设置他的名字。没有什么不同。我们在往下看我们是构造了一个Employee这个对象。然后设置姓名,------------------这是重点了------------我们有设置了Depart的属性---- 我们首先必须存入这个department对象因为这个是一的哪一方。然后我们在设置多的哪一方。这样才能成功导入、这就是插入了记录。并且两个记录是关联的。
我们再来看一下个的查询吧!
static Employee query(int empId){
Sessionsession=null;
Transactiontx=null;
try{
session=hibernateUtil.getSession();
tx=session.beginTransaction();
Employee employee=(Employee) session.get(Employee.class, empId);
System.out.println(employee.getDepart().getName());
tx.commit();
return employee;
}finally{
if (session!=null) {
session.close();
}
}
}
其他的都一样只是在加入一个函数即可就是我们的查找函数,这个函数呢?就是用员工查员工所在的部门!这样我们就能找到那个部门,如果要是差自己的编号呢?就是单表了,我们在这里用的是多表查询
这就是多对一的实例!
下面我们再来看一下一对多的实力吧!
我们还是用员工和部门这个实力吧!
是这样的我们这次用set集合为什么要用set呢?
答:在别的地方没有看到。我想应该是主键吧不能相同,而set集合正是不
能相同的元素在一起的,所以我们还是用这个set
首先我们还是来写一下department这个类吧!
package domain;
import java.util.Set;
public classDepartment {
private int id;
private String name;
private Set<Employee> 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;
}
public Set<Employee>getEmps() {
return emps;
}
public voidsetEmps(Set<Employee> emps) {
this.emps = emps;
}
}
在我们这里看来没有什么变化!只是多了一个set的集合,然后呢set集合必须指定实体就是那个Employee
我们看一下这个类所对应的xml文件吧
<hibernate-mapping package="domain">
<class name="Department">
<id name="id"column="id">
<generator class="native"/>
</id>
<property name="name" not-null="true"/>
<set name="emps">
<key column="depart_id"/>
<one-to-many class="Employee"/>
</set>
</class>
</hibernate-mapping>
分析一下其实就是多了一个set的标签吧!我们看一下内容name就是那个set的名字,里面的key标签就是那个要创建的外键?在哪里创建呢? 看下面在多的哪一方也就是Employee所对应的那个表
我们再来看一下Employee这个类
package domain;
public classEmployee {
private int id;
private String name;
private Department depart;
public Department getDepart(){
return depart;
}
public void setDepart(Departmentdepart) {
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;
}
}
看见了吧!其实是一点都没有变
在看一下那个xml文件
<hibernate-mapping package="domain"><!-- 包明子就是实体类在那个包里面 -->
<class name="Employee">
<id name="id"column="id">
<generator class="native"/>
</id>
<property name="name" not-null="true"/>
<!-- <many-to-onename="depart" column="depart_id"/> -->
</class>
</hibernate-mapping>
相信大家都看见了,原来是吧那个多对一的那个标签移除了
我们再来看两个方法 还是插入和查找的
static Department add(){
Session session=null;
Transactiontx=null;
try{
session=hibernateUtil.getSession();
tx=session.beginTransaction();
Departmentdepartment=newDepartment();
department.setName("depart name");
Employeeemployee1=newEmployee();
employee1.setName("emp name1");
employee1.setDepart(department);//建立对象模型,让两个对象关联
Employeeemployee2=newEmployee();
employee2.setName("emp name2");
employee2.setDepart(department);//建立对象模型,让两个对象关联
Set<Employee>se=newHashSet<Employee>();
se.add(employee1);
se.add(employee2);
department.setEmps(se);
session.save(employee1);
session.save(employee2);
session.save(department);
System.out.println("===========");
tx.commit();
return department;
}finally{
if (session!=null) {
session.close();
}
}
}
static DepartmentqueryDepartment(intdepartId){
Sessionsession=null;
Transactiontx=null;
try{
session=hibernateUtil.getSession();
tx=session.beginTransaction();
Departmentdepart=(Department) session.get(Department.class, departId);
System.out.println(depart.getEmps().size());
tx.commit();
return depart;
}finally{
if (session!=null) {
session.close();
}
}
}
哎!这里我们大家都看清楚了都一样吗!
但是大家在这里都要想清楚啊!
我们在多对一中查找的时候是用多的哪一方查找的少的
在一对多中查找的是后是冲少的哪一方查找多的哪一方啊!
在今天的最后我们来讲一下一对一: