一:多对一单向关联
首先我们必须创建两个实体类
例如:Dept类
public class Dept { private Integer deptNo; private String dName; private String loc; private Set<Employee> emps=new HashSet<Employee>();
Employee类
public class Employee { private Integer empno; private String ename; private Dept dept;
现在我们可以在配置文件里面配置了
Dept.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.entity"> <class name="Dept" table="Dept"> <id name="deptNo" column="deptNo"> <generator class="native"/> </id> <property name="dName" type="string" column="dName"/> <property name="loc" type="string" column="loc"/> <!--cascade:级联 inverse:反转 --> <set name="emps" cascade="save-update" inverse="true" > <key column="deptNo"></key> <!--一对多 --> <one-to-many class="Employee"/> </set> </class> </hibernate-mapping>
Employee.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.entity"> <class name="Employee" table="Employee"> <id name="empno" column="empno"> <generator class="native"/> </id> <property name="ename" type="string" column="ename"/> <!--多对一 --> <many-to-one name="dept" column="deptNo" class="Dept"></many-to-one> <set name="projects" inverse="true" table="proemp"> <key column="rempNo"></key> <many-to-many class="Project" column="rproNo"></many-to-many> </set> </class> </hibernate-mapping>
到这里,一对多 和多对一的关联 配置已经完成!!
我们一起来看看测试类
// 添加数据 private static void addDeptAndEmployee() { Session currentSession = HibernateUtil.currentSession(); currentSession.beginTransaction(); Dept dept = new Dept(); //dept.setDeptNo(deptNo) dept.setdName("质管部22"); dept.setLoc("北京22"); Employee emp = new Employee(); emp.setEname("龙龙"); emp.setDept(dept);//建立dept 到employee 对象的 关联 currentSession.save(emp); currentSession.save(dept); currentSession.getTransaction().commit(); HibernateUtil.closeSessio(); System.out.println("add ok"); }
// 级联保存 cascade:级联 private static void addDeptAndEmployeeTocascade() { Session currentSession = HibernateUtil.currentSession(); currentSession.beginTransaction(); Dept dept = new Dept(); // dept.setDeptNo(deptNo) dept.setdName("质管部22"); dept.setLoc("北京22"); Employee emp = new Employee(); emp.setEname("龙龙"); // 建立Dept对象 和Employee 对象的 一对多 双向关系 emp.setDept(dept); dept.getEmps().add(emp);// currentSession.save(dept); currentSession.getTransaction().commit(); HibernateUtil.closeSessio(); System.out.println("add ok"); }
我们谈到关联映射的多对多配置呢,有一种挺不好配置的,所以呢,就放到了这里给大家参考
第一种:建立多对多双向关联 拆成两个一对多
三个实体类
Employee
private Integer empno; private String ename; private Dept dept; );//集合 类 employee的属性 );
Project
private Integer proNo;//项目的编号 private String proName;//项目名称 private Set<Employee> employees=new HashSet<Employee>(0);//集合 类 employee的属性 private Set<ProEmp> pe=new HashSet<ProEmp>(0);
中间表
ProEmp
private Integer id; private Project pro; private Employee emp;
映射文件
Employee.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.three.entity"> <class name="Employee" table="Employee"> <id name="empno" column="empno"> <generator class="native"/> </id> <property name="ename" type="string" column="ename"/> </class> </hibernate-mapping>
Project.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.three.entity"> <class name="Project" table="Project"> <id name="proNo" column="proNo"> <generator class="native"/> </id> <property name="proName" type="string" column="proName"/> </class> </hibernate-mapping>
ProEmp.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.three.entity"> <class name="ProEmp" table="ProEmp"> <id name="id" column="id"> <generator class="native"/> </id> <!-- 与员工关联 --> <many-to-one name="emp" column="empno" class="Employee"></many-to-one> <!-- -与project关联 --> <many-to-one name="pro" class="Project" column="proNo"></many-to-one> </class> </hibernate-mapping>
大配置文件hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class"> oracle.jdbc.driver.OracleDriver </property> <property name="connection.url"> jdbc:oracle:thin:@localhost:1521:orcl </property> <property name="connection.username">wgy</property> <property name="connection.password">jpp</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --><!-- SQL 方言 --> <property name="dialect"> org.hibernate.dialect.Oracle10gDialect </property> <!-- Enable Hibernate's automatic session context management --> <!-- <property name="current_session_context_class">thread</property> --> <!-- Disable the second-level cache --> <!-- <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> --> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <!--序列化 --> <property name="hbm2ddl.auto">create</property> <!-- <property name="hbm2ddl.auto">update</property> --> <!-- sql语句格式化 --> <property name="format_sql">true</property> <!-- 资源 --> <<mapping resource="cn/three/entity/Dept.hbm.xml" /> <mapping resource="cn/three/entity/Employee.hbm.xml" /> <mapping resource="cn/three/entity/Project.hbm.xml" /> </session-factory> </hibernate-configuration>
测试类看一下效果
package cn.main; import org.hibernate.Session; import cn.three.entity.ProEmp; import cn.three.entity.Employee; import cn.three.entity.Project; import cn.util.HibernateUtil; public class ThreeTest { public static void main(String[] args) { //三表一起 threeAddEmpAndProAndProEmp(); } private static void threeAddEmpAndProAndProEmp() { Session session = HibernateUtil.currentSession(); session.beginTransaction(); Employee emp=new Employee(); Project pro=new Project(); emp.setEname("李四"); pro.setProName("一号项目"); ProEmp pe=new ProEmp(); pe.setEmp(emp); pe.setPro(pro); session.save(pro); session.save(emp); session.save(pe); session.getTransaction().commit(); HibernateUtil.closeSessio(); System.out.println("add ok"); } }
第二种:不用拆成两个一对多
只需要两个映射文件,即可
Employee.hbm.xml
复制代码 <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.entity"> <class name="Employee" table="Employee"> <id name="empno" column="empno"> <generator class="native"/> </id> <property name="ename" type="string" column="ename"/> <!--多对一 --> <many-to-one name="dept" column="deptNo" class="Dept"></many-to-one> <!--多对多双向关联 --> <set name="projects" inverse="true" table="proemp"> <key column="rempNo"></key> <many-to-many class="Project" column="rproNo"></many-to-many> </set> </class> </hibernate-mapping>
Project.hbm.xml
复制代码 <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.entity"> <class name="Project" table="Project"> <id name="proNo" column="proNo"> <generator class="native"/> </id> <property name="proName" type="string" column="proName"/> <!--配置单向多对多关联 --> <!-- 映射project 类的employees的属性 --> <!--cascade="save-update":表名保存或更新project对象时,会级联保存或更新与它关联的Employee对象 --> <set name="employees" table="proemp" cascade="save-update"> <key column="rproNo"></key> <many-to-many class="Employee" column="rempNo"></many-to-many> </set> </class> </hibernate-mapping>
测试类
复制代码 private static void addProToEmpAndEmpToPro() { Session session = HibernateUtil.currentSession(); session.beginTransaction(); Employee emp=new Employee(); Project pro=new Project(); emp.setEname("李四"); pro.setProName("一号项目"); pro.getEmployees().add(emp); session.save(pro); session.getTransaction().commit(); HibernateUtil.closeSessio(); System.out.println("add ok");
这样就可以了,有问题在线解答!!!!!