对于多对多的关联关系处理,一种方式是使用连接表来维持两者的多对多关联关系,也就是说除去两者在数据库中相应的数据表外,还会创建第三张表用来维护两者的多对多关联关系。
一。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<Wife> wifes;
public Husband(String name, List<Wife> wifes) {
this.name = name;
this.wifes = wifes;
}
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="wifes" table="couple" cascade="all"> <key column="husbandid"/> <many-to-many column="wifeid" class="com.orm.model.Wife"/> </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<Husband> husbands;
public Wife(String name) {
this.name = name;
}
public Wife(String name, List<Husband> husbands) {
this.name = name;
this.husbands = husbands;
}
}
<?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="husbands" table="couple" cascade="all"> <key column="wifeid"/> <many-to-many column="husbandid" class="com.orm.model.Husband"/> </bag> </class> </hibernate-mapping>
三。测试代码
package com.orm;
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");
Wife wife3 = new Wife("wife3");
List<Wife> wifes = new ArrayList<Wife>();
wifes.add(wife1);
wifes.add(wife2);
wifes.add(wife3);
Husband husband = new Husband("husband", wifes);
coupleService.saveOrUpdate(husband);
Husband husband1 = new Husband("husband1");
Husband husband2 = new Husband("husband2");
Husband husband3 = new Husband("husband3");
List<Husband> husbands = new ArrayList<Husband>();
husbands.add(husband1);
husbands.add(husband2);
husbands.add(husband3);
Wife wife = new Wife("wife", husbands);
coupleService.saveOrUpdate(wife);
}
}
测试结果及截图
附上源码以供参考