基于一对多和多对一,进行增删改查的操作;
每种两个实例方法,一共八种,其中:
主表:
增:对从表没有任何影响
删:在删除主表前,会先删除关联改主表的从表
改:暂时发现不能修改与从表关联的主键id
查:先查询出具体主表对象,然后出改主表关联的从表的set集合;然后进行iterator迭代遍历
从表:
增:增加一条属性,先获得需要关联的主表对象,然后在获得从表对象,把主表对象设置如从表对象属性
删:删除对主表,没有任何影响
改:修改对主表没有影响,不过若是要修改关联主表对象,需先获得新的主表对象实例,然后在获得该从表对象,在修改
查:先查询出具体的从表对象,然后通过get对象的属性方法,获得该主表对象
该实例的主表为数据库的dept部门表,从表为emp员工表
两表的序列代码:
select * from emp;
select * from dept;
create sequence emp_sequence start with 7935;
DROP sequence emp_sequence;
create sequence dept_sequence start with 50 increment by 10;
DROP sequence dept_sequence;
select dept_sequence.nextval from dual;
select emp_sequence.nextval from dual;
主要代码 :
package com.bdqn.test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;
import javax.persistence.Entity;
import org.hibernate.Session;
import com.bdqn.entitydao.BaseHibernateDAO;
import com.bdqn.entitydao.Dept;
import com.bdqn.entitydao.Emp;
@Entity
public class Test1 extends BaseHibernateDAO{
public void test1(){
//第一种,一对多查询
Session session = super.getSession();
//获取部门对象
Dept dept = (Dept)session.get(Dept.class, 10);
//查询部门为该部门的所有员工对象(无序集合)
Set<Emp> set = dept.getEmps();
//迭代
Iterator<Emp> it = set.iterator();
while (it.hasNext()) {
Emp emp = it.next();
System.out.println(emp.getEname());
}
}
public void test2(){
//第二种,多对一查询
Session session = super.getSession();
//获取员工对象
Emp emp = (Emp)getSession().get(Emp.class, Short.parseShort("7499"));
//根据获得的员工对象,可以获取到关联的部门对象(多对一)
Dept dept = emp.getDept();
System.out.println(emp.getEname()+"---"+dept.getDname());
}
public void test3(){
//第三种,增加部门(主表)不用管从表
Session session = super.getSession();
//先创建一个部门对象
Dept dept = new Dept("新增部门", "CHINA", null);
session.beginTransaction();
session.save(dept);
session.getTransaction().commit();
session.close();
}
public void test4() throws ParseException{
//第四种,增加从表
Session session = super.getSession();
//先获取一个部门对象
Dept dept = (Dept)getSession().get(Dept.class, 50);
//创建一个从表员工表的对象
Date date = new SimpleDateFormat("yyyy-MM-dd").parse("1995-7-9");
Emp emp = new Emp(dept, "JINQIAN", "CLERK", Short.parseShort("7902"), date, 7000.00, 1000.00);
session.beginTransaction();
session.save(emp);
session.getTransaction().commit();
session.close();
}
public void test5() throws ParseException{
//第四种,修改从表
Session session = super.getSession();
//先获取从表
Emp emp = (Emp)session.get(Emp.class, Short.parseShort("60"));
//然后获取需要更改后的主表
Dept dept = (Dept)session.get(Dept.class, 40);
emp.setDept(dept);
session.beginTransaction();
session.beginTransaction();
session.getTransaction().commit();
session.close();
}
public void test6() throws ParseException{
//第五种,修改主表
Session session = super.getSession();
//获取主表
Dept dept = (Dept)session.get(Dept.class, 50);
dept.setDname("bumen");
//不能修改主表与从表关联的deptno
session.beginTransaction();
session.getTransaction().commit();
session.close();
}
public void test7() throws ParseException{
//第六种,删除从表
Session session = super.getSession();
//获得要删除的从表,主表没关系
Emp emp = (Emp)session.get(Emp.class, Short.parseShort("60"));
session.beginTransaction();
session.delete(emp);
session.getTransaction().commit();
session.close();
}
public void test8() throws ParseException{
//第六种,删除主表
Session session = super.getSession();
//获得要删除的主表:在主表的set配置中设置cascade级联属性=all
Dept dept = (Dept)session.get(Dept.class, 50);
session.beginTransaction();
session.delete(dept);
session.getTransaction().commit();
session.close();
// Hibernate: select dept0_.DEPTNO as DEPTNO1_0_, dept0_.DNAME as DNAME1_0_, dept0_.LOC as LOC1_0_ from SYSTEM.DEPT dept0_ where dept0_.DEPTNO=?
// Hibernate: select emps0_.DEPTNO as DEPTNO1_1_, emps0_.EMPNO as EMPNO1_, emps0_.EMPNO as EMPNO0_0_, emps0_.DEPTNO as DEPTNO0_0_, emps0_.ENAME as ENAME0_0_, emps0_.JOB as JOB0_0_, emps0_.MGR as MGR0_0_, emps0_.HIREDATE as HIREDATE0_0_, emps0_.SAL as SAL0_0_, emps0_.COMM as COMM0_0_ from SYSTEM.EMP emps0_ where emps0_.DEPTNO=?
// Hibernate: delete from SYSTEM.EMP where EMPNO=?
// Hibernate: delete from SYSTEM.DEPT where DEPTNO=?
}
public static void main(String[] args) throws ParseException {
Test1 test1 = new Test1();
test1.test8();
}
}
实体层代码:
package com.bdqn.entitydao;
import java.util.HashSet;
import java.util.Set;
/**
* Dept entity. @author MyEclipse Persistence Tools
*/
public class Dept implements java.io.Serializable {
// Fields
private Integer deptno;
private String dname;
private String loc;
private Set emps = new HashSet(0);
// Constructors
/** default constructor */
public Dept() {
}
/** full constructor */
public Dept(String dname, String loc, Set emps) {
this.dname = dname;
this.loc = loc;
this.emps = emps;
}
// Property accessors
public Integer getDeptno() {
return this.deptno;
}
public void setDeptno(Integer deptno) {
this.deptno = deptno;
}
public String getDname() {
return this.dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return this.loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
public Set getEmps() {
return this.emps;
}
public void setEmps(Set emps) {
this.emps = emps;
}
}
package com.bdqn.entitydao;
import java.util.Date;
/**
* Emp entity. @author MyEclipse Persistence Tools
*/
public class Emp implements java.io.Serializable {
// Fields
private Short empno;
private Dept dept;
private String ename;
private String job;
private Short mgr;
private Date hiredate;
private Double sal;
private Double comm;
// Constructors
/** default constructor */
public Emp() {
}
/** full constructor */
public Emp(Dept dept, String ename, String job, Short mgr, Date hiredate,
Double sal, Double comm) {
this.dept = dept;
this.ename = ename;
this.job = job;
this.mgr = mgr;
this.hiredate = hiredate;
this.sal = sal;
this.comm = comm;
}
// Property accessors
public Short getEmpno() {
return this.empno;
}
public void setEmpno(Short empno) {
this.empno = empno;
}
public Dept getDept() {
return this.dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
public String getEname() {
return this.ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return this.job;
}
public void setJob(String job) {
this.job = job;
}
public Short getMgr() {
return this.mgr;
}
public void setMgr(Short mgr) {
this.mgr = mgr;
}
public Date getHiredate() {
return this.hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
public Double getSal() {
return this.sal;
}
public void setSal(Double sal) {
this.sal = sal;
}
public Double getComm() {
return this.comm;
}
public void setComm(Double comm) {
this.comm = comm;
}
}
表xml配置
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.bdqn.entitydao.Dept" table="DEPT" schema="SYSTEM">
<id name="deptno" type="java.lang.Integer">
<column name="DEPTNO" precision="6" scale="0" />
<generator class="sequence">
<param name="sequence">dept_sequence</param>
</generator>
</id>
<property name="dname" type="java.lang.String">
<column name="DNAME" length="14" />
</property>
<property name="loc" type="java.lang.String">
<column name="LOC" length="13" />
</property>
<set name="emps" inverse="true" cascade="all">
<key>
<column name="DEPTNO" precision="2" scale="0" />
</key>
<one-to-many class="com.bdqn.entitydao.Emp" />
</set>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.bdqn.entitydao.Emp" table="EMP" schema="SYSTEM">
<id name="empno" type="java.lang.Short">
<column name="EMPNO" precision="4" scale="0" />
<generator class="sequence">
<param name="sequence">emp_sequence</param>
</generator>
</id>
<many-to-one name="dept" class="com.bdqn.entitydao.Dept" fetch="select">
<column name="DEPTNO" precision="2" scale="0" />
</many-to-one>
<property name="ename" type="java.lang.String">
<column name="ENAME" length="10" />
</property>
<property name="job" type="java.lang.String">
<column name="JOB" length="9" />
</property>
<property name="mgr" type="java.lang.Short">
<column name="MGR" precision="4" scale="0" />
</property>
<property name="hiredate" type="java.util.Date">
<column name="HIREDATE" length="7" />
</property>
<property name="sal" type="java.lang.Double">
<column name="SAL" precision="7" />
</property>
<property name="comm" type="java.lang.Double">
<column name="COMM" precision="7" />
</property>
</class>
</hibernate-mapping>