Hibernate ORM - 一对多双向关联关系(我是韦小宝)

来源:互联网 发布:淘宝显示几人付款 编辑:程序博客网 时间:2024/04/20 02:07

  对于一对多的关联关系,通常的做法就是在多方映射的数据表中增加一个外键字段,用以保持一方的主键标识符,维持一对多的关联关系。

 

  一。Husband

 

Java代码  收藏代码
  1. package com.orm.model;  
  2.   
  3. import java.util.List;  
  4.   
  5. /** 
  6.  * Created by IntelliJ IDEA. 
  7.  * User: Zhong Gang 
  8.  * Date: 10/18/11 
  9.  * Time: 3:23 PM 
  10.  */  
  11. public class Husband extends DomainObject {  
  12.     private String name;  
  13.   
  14.     private List<Wife> wifes;  
  15.   
  16.     public Husband(String name, List<Wife> wifes) {  
  17.         this.name = name;  
  18.         this.wifes = wifes;  
  19.     }  
  20. }  

 

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.         "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  5.   
  6. <hibernate-mapping default-access="field">  
  7.   
  8.     <class name="com.orm.model.Husband" table="husband">  
  9.         <id name="id" column="id" type="java.lang.Integer">  
  10.             <generator class="native"/>  
  11.         </id>  
  12.         <property name="name" column="name" type="java.lang.String"/>  
  13.   
  14.         <bag name="wifes" cascade="all">  
  15.             <key column="husbandid"/>  
  16.             <one-to-many class="com.orm.model.Wife"/>  
  17.         </bag>  
  18.     </class>  
  19.   
  20. </hibernate-mapping>  
 

  二。Wife

 

Java代码  收藏代码
  1. package com.orm.model;  
  2.   
  3. /** 
  4.  * Created by IntelliJ IDEA. 
  5.  * User: Zhong Gang 
  6.  * Date: 10/18/11 
  7.  * Time: 3:23 PM 
  8.  */  
  9. public class Wife extends DomainObject {  
  10.     private String name;  
  11.   
  12.     private Husband husband;  
  13.   
  14.     public Wife(String name) {  
  15.         this.name = name;  
  16.     }  
  17. }  

 

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.         "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  5.   
  6. <hibernate-mapping default-access="field">  
  7.   
  8.     <class name="com.orm.model.Wife" table="wife">  
  9.         <id name="id" column="id" type="java.lang.Integer">  
  10.             <generator class="native"/>  
  11.         </id>  
  12.         <property name="name" column="name" type="java.lang.String"/>  
  13.   
  14.         <many-to-one name="husband" column="husbandid" class="com.orm.model.Husband"/>  
  15.     </class>  
  16.   
  17. </hibernate-mapping>  

 

  三。测试代码

 

Java代码  收藏代码
  1. package com.orm;  
  2.   
  3. import com.orm.model.Husband;  
  4. import com.orm.model.Wife;  
  5. import com.orm.service.CoupleService;  
  6. import junit.framework.TestCase;  
  7. import org.springframework.context.ApplicationContext;  
  8. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  9.   
  10. import java.util.ArrayList;  
  11. import java.util.List;  
  12.   
  13. /** 
  14.  * Created by IntelliJ IDEA. 
  15.  * User: Zhong Gang 
  16.  * Date: 10/18/11 
  17.  * Time: 3:40 PM 
  18.  */  
  19. public class HibernateOneToManyTest extends TestCase {  
  20.     private CoupleService coupleService;  
  21.   
  22.     @Override  
  23.     public void setUp() throws Exception {  
  24.         ApplicationContext context = new ClassPathXmlApplicationContext("classpath:testDataSource.xml");  
  25.         coupleService = (CoupleService) context.getBean("coupleService");  
  26.     }  
  27.   
  28.     public void testOneToOne() throws Exception {  
  29.         Wife wife1 = new Wife("wife1");  
  30.         Wife wife2 = new Wife("wife2");  
  31.         Wife wife3 = new Wife("wife3");  
  32.   
  33.         List<Wife> wifes = new ArrayList<Wife>();  
  34.         wifes.add(wife1);  
  35.         wifes.add(wife2);  
  36.         wifes.add(wife3);  
  37.   
  38.         Husband husband = new Husband("husband", wifes);  
  39.         coupleService.saveOrUpdate(husband);  
  40.     }  
  41. }  

 

  测试结果截图

 

  sql

 

  table

 

  这是一个典型的一对多的双向关联关系,如果你需要的只是一对多的单向关联关系,删除任何一方对另一方的引用的同时删除相应配置文件中的many-to-one或者相应的集合元素即可。最后附上源码以供参考。