您的位置:首页 > 其它

hibernate系列八:一对多关联配置及案例

2017-11-29 22:42 363 查看
接上一篇的多对一案例,不多说。

=================实体类DeptEntity.java=======================

package com.obtk.entitys;

import java.util.ArrayList;
import java.util.List;

public class DeptEntity implements java.io.Serializable {

private static final long serialVersionUID = -8998119551638998375L;
private Integer deptId;
private String departName;
private String address;
//建立和学生的一对多关联
private List<StudentEntity> stuList=new ArrayList<StudentEntity>();

public DeptEntity() {
}

public DeptEntity(Integer deptId, String departName, String address) {
this.deptId = deptId;
this.departName = departName;
this.address = address;
}

public Integer getDeptId() {
return this.deptId;
}

public void setDeptId(Integer deptId) {
this.deptId = deptId;
}

public String getDepartName() {
return this.departName;
}

public void setDepartName(String departName) {
this.departName = departName;
}

public String getAddress() {
return this.address;
}

public void setAddress(String address) {
this.address = address;
}

public void setStuList(List<StudentEntity> stuList) {
this.stuList = stuList;
}
public List<StudentEntity> getStuList() {
return stuList;
}

}===========================映射文件Department.hbm.xml============================
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.obtk.entitys">
<class name="DeptEntity" table="department" >
<id name="deptId" type="integer">
<column name="deptId" />
<generator class="assigned"></generator>
</id>
<property name="departName" type="java.lang.String">
<column name="departName" length="20" />
</property>
<property name="address" type="java.lang.String">
<column name="address" length="20" />
</property>
<!-- cascade表示级联,inserse表示控制反转,设为true在有些场合下能提高性能
order-by 表示关联的集合元素按照一定条件进行排序
column="deptIdd"表示外键表的外键,这里千万不要搞成主键了,注意!
-->
<bag name="stuList" cascade="all" inverse="true" order-by="age desc">
<key column="deptIdd"></key>
<one-to-many class="StudentEntity"/>
</bag>
</class>
</hibernate-mapping>
接下来是案例
案例一.   数据添加

package com.obtk.test;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.obtk.entitys.DeptEntity;
import com.obtk.entitys.StudentEntity;
import com.obtk.utils.HiberUtil;

public class AddTest2 {
public static void main(String[] args) {
Session session=null;
Transaction tx=null;
try {
session=HiberUtil.getSession();
tx=session.beginTransaction();
DeptEntity dept=new DeptEntity(15, "金融系", "金融楼320");
//session.save(dept); //有瞬时态转换为持久态
StudentEntity stu1=new StudentEntity("aa", "男", 21, "obtk学生宿舍");
StudentEntity stu2=new StudentEntity("cc", "女", 20, "obtk学生宿舍");
stu1.setDept(dept);
stu2.setDept(dept);
dept.getStuList().add(stu1);
dept.getStuList().add(stu2);
//如果配置了cascade属性,这里可以去掉,表示可以级联添加
//session.save(stu1);
//session.save(stu2);
session.save(dept);
tx.commit();
} catch (HibernateException e) {
tx.rollback();
e.printStackTrace();
}finally{
HiberUtil.closeSession();
}
}
}


案例二.    删除操作
package com.obtk.test;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.obtk.entitys.DeptEntity;
import com.obtk.entitys.StudentEntity;
import com.obtk.utils.HiberUtil;

public class DeleteTest1 {
public static void main(String[] args) {
Session session=null;
Transaction tx=null;
try {
session=HiberUtil.getSession();
tx=session.beginTransaction();
DeptEntity dept=(DeptEntity)session.load(DeptEntity.class, 15);
//如果配置了级联属性,这里就会级联删除该系下所有学生
session.delete(dept);
tx.commit();
} catch (HibernateException e) {
tx.rollback();
e.printStackTrace();
}finally{
HiberUtil.closeSession();
}
}
}


案例三.   查询
package com.obtk.test;

import java.util.List;
import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.Session;

import com.obtk.entitys.DeptEntity;
import com.obtk.entitys.StudentEntity;
import com.obtk.utils.HiberUtil;

public class JoinTest2 {
public static void main(String[] args) {
Session session=null;
try {
session=HiberUtil.getSession();
DeptEntity dept=(DeptEntity)session.load(DeptEntity.class, 10);
List<StudentEntity> students=dept.getStuList();
//如果配置order by 属性,这里的结果是安装age排序的,请参照配置文件
for(StudentEntity stu : students){
System.out.println(stu.getStuName()+"\t"+stu.getGender()
+"\t"+dept.getDepartName()+"\t"+stu.getAge());
}
} catch (HibernateException e) {
e.printStackTrace();
}finally{
HiberUtil.closeSession();
}
}
}
案例4.  更新操作
package com.obtk.test;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.obtk.entitys.DeptEntity;
import com.obtk.entitys.StudentEntity;
import com.obtk.utils.HiberUtil;

public class UpdateTest1 {
public static void main(String[] args) {
Session session=null;
Transaction tx=null;
try {
session=HiberUtil.getSession();
tx=session.beginTransaction();
DeptEntity dept=(DeptEntity)session.load(DeptEntity.class, 10);
StudentEntity stu=(StudentEntity)session.load(StudentEntity.class, 130);
stu.setStuName("小红");
stu.setDept(dept); //更改了系
dept.getStuList().add(stu); //从一方也做关联
tx.commit();
} catch (HibernateException e) {
tx.rollback();
e.printStackTrace();
}finally{
HiberUtil.closeSession();
}
}
}
如果在bag标签里配置了inverse=true属性,那么这里更新性能会比较高,少输出了sql语句。

根据上述实验,可以得到这样的结论。

    (1)在映射一对多的双向关联关系时,应该在“one”方把<set>元素的inverse属性设为true,以提高应用的性能。

    (2)在建立两个对象的双向关联时,应该同时修改关联两端的对象的相应属性:

    student.setDept(dept);

    dept.getStudents().add(student);

    这样才会使程序更加健壮,提高业务逻辑层的独立性,使业务逻辑层的代码不受Hibernate实现的影响。同理,当解除双向关联的关系时,也应该修改关联两端的对象的相应属性:

    student.setDept(dept);

    dept.getStudents().remove(student);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: