当关联关系配置双向时,一方的set集合中inverse属性应设置为true。此时在级联操作时应将双方对象设置关联。
若关联关系为单向一对多时,inverse属性不可设置为true,用于保障级联操作时外键完整性。(和查询没关系,)
一:创建表
-- 创建部门和员工信息表
create table dept
(
did number(7) primary key,
deptName varchar2(20) not null
);
create table emp
(
eid number(7) primary key,
empName varchar2(20) not null,
deptid number(7) references dept(did)
);
create sequence seq_dept;
create sequence seq_emp;
insert into dept values(seq_dept.nextval,'开发部');
insert into dept values(seq_dept.nextval,'测试部');
insert into dept values(seq_dept.nextval,'销售部');
commit;
insert into emp values(seq_emp.nextval,'张三',1);
insert into emp values(seq_emp.nextval,'李四',1);
insert into emp values(seq_emp.nextval,'王五',2);
insert into emp values(seq_emp.nextval,'赵六',2);
insert into emp values(seq_emp.nextval,'孙七',3);
insert into emp values(seq_emp.nextval,'老八',3);
insert into emp values(seq_emp.nextval,'小九',3);
commit;
select * from dept;
select * from emp;
select * from emp where eid = 4
select * from dept where did = 2
insert int dept values(5,'行政部');
insert int emp values(10,'行政人员1',null);
insert int emp values(11,'行政人员1',null);
update emp set deptid = 5 where eid = 10;
update emp set deptid = 5 where eid = 11;
实体类
package entity;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
/*
* hibernate实体类
*/
public class Dept implements Serializable {
private Integer did;
private String deptName;
//增加关联集合属性(一对多)//在1的一方,表达持有多的一方的引用=》使用集合(一个部门有多个人员)(这个在一对多的时候用的。比如查找某个部门下的员工)
private Set<Emp> empSet=new HashSet<Emp>() ;//查找某个员工在那个部门用不到这个(多对一)
public Dept() {
super();
// TODO Auto-generated constructor stub
}
public Dept(Integer did, String deptName, Set<Emp> empSet) {
super();
this.did = did;
this.deptName = deptName;
this.empSet = empSet;
}
public Integer getDid() {
return did;
}
public void setDid(Integer did) {
this.did = did;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public Set<Emp> getEmpSet() {
return empSet;
}
public void setEmpSet(Set<Emp> empSet) {
this.empSet = empSet;
}
}
emp实体类
package entity;
import java.io.Serializable;
public class Emp implements Serializable {
private Integer eid;
private String empName;
//private Dept dept; 外键,在这里不用这,直接管理另一个类的属性
private Dept dept;//关联属性
public Emp(){
}
public Emp(Integer eid, String empName, Dept dept) {
super();
this.eid = eid;
this.empName = empName;
this.dept = dept;
}
public Integer getEid() {
return eid;
}
public void setEid(Integer eid) {
this.eid = eid;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
}
二:配置文件
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="connection.username">system</property>
<property name="connection.password">password</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping resource="entity/Dept.hbm.xml"/><!-- 这个是写好Emp.hbm.xml配置文件后在写回来 -->
<mapping resource="entity/Emp.hbm.xml"/>
</session-factory>
</hibernate-configuration>
三:配置实体类与表的映射文件(本次测试是:emp表与的dept表,emp表中引用了dept表的主键作为外键。测试可以通过查询emp表查找某个人员在哪个部门
)
在emp表中设置一对多的映射关系。多个员工对应一个部门。 many—to-one name是部门表的表名(多员工对应一个部门,哪个one后面就是要对应的一的那个部门表 class 也是部门表的实体类。注意那个column里写的是emp表中的外键)
<?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>
<class name="entity.Emp" table="Emp">
<id name="eid" >
<generator class="sequence">
<param name="sequence">seq_emp</param>
</generator>
</id>
<property name="empName"></property>
<!--配置多对一映射关系 --> <!-- emp表中的外键 -->
<many-to-one name="dept" class="entity.Dept" column="DEPTID"/>
</class>
</hibernate-mapping>
dept表
<?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>
<class name="entity.Dept" table="dept">
<id name="did" column="did ">
<generator class="sequence">
<param name="sequence">
seq_dept
</param>
</generator>
</id>
<property name="deptName"></property> <!-- 这个属性不要写错了是property -->
</class>
</hibernate-mapping>
测试类
package Test;
import org.hibernate.Session;
import entity.Emp;
import Util.HibernateUtil;
public class Test {
public static void main(String[] args) {
Session session = HibernateUtil.currentSesion();
Emp emp=(Emp) session.load(Emp.class,4);
System.out.println(emp.getEmpName()+"所在部门为:"+emp.getDept().getDeptName());
}
}