package org.niit.ormlab.example3; import java.io.Serializable; import java.util.HashSet; import java.util.Set; public class Depart implements Serializable { private Integer did; private String dname; private String ddesc; private Set<Emp> emps = new HashSet<Emp>();//为了提高程序健壮性我们实例化这个属性避免产生空指针异常 public Depart(String dname, String ddesc, Set<Emp> emps) { super(); this.dname = dname; this.ddesc = ddesc; this.emps = emps; } public Set<Emp> getEmps() { return emps; } public void setEmps(Set<Emp> emps) { this.emps = emps; } public Depart() { } public Depart(String dname, String ddesc) { super(); this.dname = dname; this.ddesc = ddesc; } public Depart(Integer did, String dname, String ddesc) { super(); this.did = did; this.dname = dname; this.ddesc = ddesc; } public Integer getDid() { return did; } public void setDid(Integer did) { this.did = did; } public String getDname() { return dname; } public void setDname(String dname) { this.dname = dname; } public String getDdesc() { return ddesc; } public void setDdesc(String ddesc) { this.ddesc = ddesc; } } package org.niit.ormlab.example3; import java.io.Serializable; import java.util.*; public class Emp implements Serializable { private int eid; private String ename; private String esex; private String eaddr; private String etel; private Depart depart;//many to one private Set<Role> roles = new HashSet<Role>();//多对多 public Emp() { // TODO Auto-generated constructor stub } public Emp(String ename, String esex, String eaddr, String etel, Depart depart, Set<Role> roles) { super(); this.ename = ename; this.esex = esex; this.eaddr = eaddr; this.etel = etel; this.depart = depart; this.roles = roles; } public Set<Role> getRoles() { return roles; } public void setRoles(Set<Role> roles) { this.roles = roles; } public Emp(int eid, String ename, String esex, String eaddr, String etel, Depart depart, Set<Role> roles) { super(); this.eid = eid; this.ename = ename; this.esex = esex; this.eaddr = eaddr; this.etel = etel; this.depart = depart; this.roles = roles; } public Emp(String ename, String esex, String eaddr, String etel, Depart depart) { super(); this.ename = ename; this.esex = esex; this.eaddr = eaddr; this.etel = etel; this.depart = depart; } public Emp(int eid, String ename, String esex, String eaddr, String etel, Depart depart) { super(); this.eid = eid; this.ename = ename; this.esex = esex; this.eaddr = eaddr; this.etel = etel; this.depart = depart; } public int getEid() { return eid; } public void setEid(int eid) { this.eid = eid; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public String getEsex() { return esex; } public void setEsex(String esex) { this.esex = esex; } public String getEaddr() { return eaddr; } public void setEaddr(String eaddr) { this.eaddr = eaddr; } public String getEtel() { return etel; } public void setEtel(String etel) { this.etel = etel; } public Depart getDepart() { return depart; } public void setDepart(Depart depart) { this.depart = depart; } } package org.niit.ormlab.example3; import java.io.Serializable; import java.util.*; public class Role implements Serializable { private int rid; private String rname; private String rdesc; private Set<Emp> emps = new HashSet<Emp>();//many to many public Role(String rname, String rdesc) { super(); this.rname = rname; this.rdesc = rdesc; } public Role(String rname, String rdesc, Set<Emp> emps) { super(); this.rname = rname; this.rdesc = rdesc; this.emps = emps; } public Role() { super(); } public Role(int rid, String rname, String rdesc, Set<Emp> emps) { super(); this.rid = rid; this.rname = rname; this.rdesc = rdesc; this.emps = emps; } public int getRid() { return rid; } public void setRid(int rid) { this.rid = rid; } public String getRname() { return rname; } public void setRname(String rname) { this.rname = rname; } public String getRdesc() { return rdesc; } public void setRdesc(String rdesc) { this.rdesc = rdesc; } public Set<Emp> getEmps() { return emps; } public void setEmps(Set<Emp> emps) { this.emps = emps; } } <?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="org.niit.ormlab.example3"> <!-- 多对一的【一方】 --> <class name="Depart" table="`Depart`"> <id name="did" column="did" type="integer"> <generator class="identity" /> </id> <property name="dname" column="dname" type="string" /> <property name="ddesc" column="ddesc" type="string" /> <!-- 级联保存更新 --> <set name="emps" table="`Emp`" cascade="save-update,delete"> <!-- 外键信息 --> <key column="did"></key> <!-- 属性对应的类信息 --> <one-to-many class="Emp"/> </set> </class> </hibernate-mapping> <?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="org.niit.ormlab.example3"> <!-- 多对一的【多方】 --> <class name="Emp" table="`Emp`"> <id name="eid" column="eid" type="integer"> <generator class="identity" /> </id> <property name="ename" column="ename" type="string" /> <property name="esex" column="esex" type="string" /> <property name="etel" column="`etel`" type="string"/> <property name="eaddr" column="eaddr" type="string" /> <!-- many-to-one建立depart属性和外键did之间的映射 name=待映射的持久化类的属性名【Emp类的depart属性】 class=持久化类名 cascade=级联行为 not-null=是否允许为空,影响Hibernate的保存时行为 --> <many-to-one name="depart" class="Depart" column="did" cascade="save-update" not-null="true"/> <set name="roles" cascade="save-update" table="`roleemp`" lazy="true" inverse="true" > <key column="eid" not-null="true"></key> <many-to-many class="Role" column="rid"/> </set> </class> </hibernate-mapping> <?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="org.niit.ormlab.example3"> <!-- 多对一的【多方】 --> <class name="Role" table="`Role`"> <id name="rid" column="rid" type="integer"> <generator class="identity" /> </id> <property name="rname"></property> <property name="rdesc"></property> <set name="emps" cascade="save-update" lazy="true" table="roleemp" > <key column="rid" not-null="true"></key> <many-to-many class="Emp" column="eid" /> </set> </class> </hibernate-mapping> 测试代码: package org.niit.ormlab.example3; import java.util.List; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.criterion.Restrictions; public class Client { /**保存角色和员工使用级联*/ public void saveRoleAndEmpWithCascade(){ Session session = SessionFactory.getSession(); Transaction ts = session.beginTransaction(); try { Depart depart = (Depart)session.get(Depart.class,19); Role r = new Role("市场经理","市场经理"); Emp e1 = new Emp("王涛", "男", "成都市武侯区", "115", depart); Emp e2 = new Emp("王丽", "女", "成都市武侯区", "116", depart); //建立角色对象和员工对象的双向关系 r.getEmps().add(e1); r.getEmps().add(e2); //e1.getRoles().add(r);//在Role的映射上使用了cascade=save-update可以省略这一句代码 //e2.getRoles().add(r);//在Role的映射上使用了cascade=save-update可以省略这一句代码 session.save(r);//保存角色对象 ts.commit(); } catch (Exception e) { e.printStackTrace(); ts.rollback(); } finally { session.close(); } } /** * 得到角色信息及该角色的员工信息 * */ public void getRoleAndEmps(){ Session session = SessionFactory.getSession(); try { Role r = (Role)session .createQuery("from Role as r where r.rid=?") .setParameter(0, 3) .uniqueResult(); System.out.println(r.getRid()+"/t"+r.getRname()); for (Emp e : r.getEmps()) { System.out.println("/t"+e.getEid()+"/t"+e.getEname()); } } catch (Exception e) { e.printStackTrace(); } finally { session.close(); } } public static void main(String[] args) { Client c = new Client(); //c.saveRoleAndEmpWithCascade(); c.getRoleAndEmps(); } }