我們談到關聯映射的多對多配置呢,有一種挺不好配置的,所以呢,就放到了這裡給大家參考
第一種:建立多對多雙向關聯 拆成兩個一對多
三個實體類
Employee
private Integer empno; private String ename; private Dept dept; private Set<Project> projects=new HashSet<Project>(0);//集合 類 employee的屬性 private Set<ProEmp> pe=new HashSet<ProEmp>(0);
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");
這樣就可以了,有問題在線解答!!!!!11111111