对于多对多的关联关系处理,前面我讲解了在数据库中建立第三张表用于维护两者的关联关系,现在我来讲解另一种比较常用且通用的处理方式,那就是将这个多对多的关联关系拆解成两个一对多的关联关系,意取从这个多对多的关联关系中提取出一个中间实体,用来表示两者的多对多关联关系,这个中间实体在数据库中存在相应的数据表映射。
一。Husband
package com.orm.model;
import java.util.List;
/**
* Created by IntelliJ IDEA.
* User: Zhong Gang
* Date: 10/18/11
* Time: 3:23 PM
*/
public class Husband extends DomainObject {
private String name;
private List<Couple> couples;
public Husband(String name) {
this.name = name;
}
}
<?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 default-access="field"> <class name="com.orm.model.Husband" table="husband"> <id name="id" column="id" type="java.lang.Integer"> <generator class="native"/> </id> <property name="name" column="name" type="java.lang.String"/> <bag name="couples" cascade="all"> <key column="husbandid"/> <one-to-many class="com.orm.model.Couple"/> </bag> </class> </hibernate-mapping>
二。Wife
package com.orm.model;
import java.util.List;
/**
* Created by IntelliJ IDEA.
* User: Zhong Gang
* Date: 10/18/11
* Time: 3:23 PM
*/
public class Wife extends DomainObject{
private String name;
private List<Couple> couples;
public Wife(String name) {
this.name = name;
}
}
<?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 default-access="field"> <class name="com.orm.model.Wife" table="wife"> <id name="id" column="id" type="java.lang.Integer"> <generator class="native"/> </id> <property name="name" column="name" type="java.lang.String"/> <bag name="couples" cascade="all"> <key column="wifeid"/> <one-to-many class="com.orm.model.Couple"/> </bag> </class> </hibernate-mapping>
三。Couple
package com.orm.model;
import java.util.List;
/**
* Created by IntelliJ IDEA.
* User: Zhong Gang
* Date: 10/18/11
* Time: 3:23 PM
*/
public class Couple extends DomainObject{
private Husband husband;
private Wife wife;
public Couple(Husband husband, Wife wife) {
this.husband = husband;
this.wife = wife;
}
}
<?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 default-access="field"> <class name="com.orm.model.Couple" table="couple"> <id name="id" column="id" type="java.lang.Integer"> <generator class="native"/> </id> <many-to-one name="husband" class="com.orm.model.Husband" column="husbandid"/> <many-to-one name="wife" class="com.orm.model.Wife" column="wifeid"/> </class> </hibernate-mapping>
四。测试代码
package com.orm;
import com.orm.model.Couple;
import com.orm.model.Husband;
import com.orm.model.Wife;
import com.orm.service.CoupleService;
import junit.framework.TestCase;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.ArrayList;
import java.util.List;
/**
* Created by IntelliJ IDEA.
* User: Zhong Gang
* Date: 10/18/11
* Time: 3:40 PM
*/
public class HibernateManyToManyTest extends TestCase {
private CoupleService coupleService;
@Override
public void setUp() throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:testDataSource.xml");
coupleService = (CoupleService) context.getBean("coupleService");
}
public void testManyToMany() throws Exception {
Wife wife1 = new Wife("wife1");
Wife wife2 = new Wife("wife2");
coupleService.saveOrUpdate(wife1);
coupleService.saveOrUpdate(wife2);
Husband husband1 = new Husband("husband1");
Husband husband2 = new Husband("husband2");
coupleService.saveOrUpdate(husband1);
coupleService.saveOrUpdate(husband2);
Couple couple1 = new Couple(husband1, wife1);
Couple couple2 = new Couple(husband1, wife2);
Couple couple3 = new Couple(husband2, wife1);
Couple couple4 = new Couple(husband2, wife2);
coupleService.saveOrUpdate(couple1);
coupleService.saveOrUpdate(couple2);
coupleService.saveOrUpdate(couple3);
coupleService.saveOrUpdate(couple4);
}
}
测试结果截图
最后附上源码以供参考