多对多的实例很多,这里说一个比较简单的例子,而且还是单向引用.. 比如说一个人可以对应多个地址,一个地址可以被多个人所拥有..
这就是对多多的关联关系,
在做多对多的关联的时候,一定要注意,别遗漏 中间表的配置。
下面是Pojo的类:
Person_FK_Many_to_Many:
public class Person_FK_Many_to_Many implements Serializable{ private static final long serialVersionUID = -7132895885951796381L; private Integer id; private String name; private Integer age; private Set<Address_FK_Many_to_Many> addressFKManyToManieSet = new HashSet<Address_FK_Many_to_Many>(); //getter 和setter方法省略; }
Address_FK_Many_to_Many:
public class Address_FK_Many_to_Many { private Integer addressID; private String addressDetail; //getter和setter方法省略; }
下面是Hibernate配置代码:
Person_FK_Many_to_Many.hbm.xml:
<?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> <class name="pack.java.model.Person_FK_Many_to_Many" table="ZHT_PERSON_FK_MANY_TO_MANY"> <id name="id"> <generator class="identity"/> </id> <property name="name" column="NAME"></property> <property name="age" column="AGE"></property> <!-- ZHT_PERSON_ADDRESS_MANY_TO_MANY 这个set中的table,是指映射到数据库中的中间表; 用来存储personID 和 AddressID。 --> <set name="addressFKManyToManieSet" lazy="false" table="ZHT_PERSON_ADDRESS_MANY_TO_MANY" inverse="false" cascade="all"> <key column="id" not-null="false" ></key> <many-to-many column="addressID" class="pack.java.model.Address_FK_Many_to_Many"> </many-to-many> </set> </class> </hibernate-mapping>
Address_FK_Many_to_Many.hbm.xml
<?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> <class name="pack.java.model.Address_FK_Many_to_Many" table="ZHT_ADDRESS_FK_MANY_TO_MANY"> <id name="addressID"> <generator class="identity"></generator> </id> <property name="addressDetail" column="addressDetail"></property> </class> </hibernate-mapping>
然后再Hibernate.cfg.xml中,把刚刚建立的两个*.hbm.xml文件。
<mapping resource="pack/java/model/Address_FK_Many_to_Many.hbm.xml"/>
<mapping resource="pack/java/model/Person_FK_Many_to_Many.hbm.xml"/>
最后,就写一个测试类,来测试刚刚配置的单向多对多的关联关系.
package pack.java.test; import java.util.List; import java.util.Set; import org.hibernate.Query; import org.hibernate.Session; import pack.java.hibernate.HibernateSessionFactory; import pack.java.model.Address_FK_Many_to_Many; import pack.java.model.Person_FK_Many_to_Many; /** * 一个人可以对应多个地址,一个地址,也可以对应多个人。 * @author Administrator * */ public class HibernateDemoManyToMany { private Session getSession(){ Session session = HibernateSessionFactory.getSession(); return session; } public static void main(String[] args) { HibernateDemoManyToMany demoManyToMany = new HibernateDemoManyToMany(); Session session = demoManyToMany.getSession(); //保存; demoManyToMany.savePerson(session); //查询; demoManyToMany.queryPerson(session); } private void queryPerson(Session session){ //Person_FK_Many_to_Many personFKManyToMany = (Person_FK_Many_to_Many) session.load(Person_FK_Many_to_Many.class, 1); String hql = "select p from Person_FK_Many_to_Many p"; Query query = session.createQuery(hql); List<Person_FK_Many_to_Many> list = query.list(); for(Person_FK_Many_to_Many personFKManyToMany:list){ System.out.println(personFKManyToMany.getId()+","+personFKManyToMany.getName()+","+personFKManyToMany.getAge()); Set<Address_FK_Many_to_Many> addressFKManyToManies = personFKManyToMany.getAddressFKManyToManieSet(); for (Address_FK_Many_to_Many addressFKManyToMany : addressFKManyToManies) { System.out.println(addressFKManyToMany.getAddressID()+","+addressFKManyToMany.getAddressDetail()); } } session.close(); System.out.println(list.get(0).getAddressFKManyToManieSet().size()); } private void savePerson(Session session){ Person_FK_Many_to_Many fkManyToMany1 = new Person_FK_Many_to_Many(); fkManyToMany1.setAge(22); fkManyToMany1.setName("ZhouHaiTao"); Person_FK_Many_to_Many fkManyToMany2 = new Person_FK_Many_to_Many(); fkManyToMany2.setAge(23); fkManyToMany2.setName("ZhangSan"); Address_FK_Many_to_Many addressFKManyToMany1 = new Address_FK_Many_to_Many(); addressFKManyToMany1.setAddressDetail("北京市朝阳区"); Address_FK_Many_to_Many addressFKManyToMany2 = new Address_FK_Many_to_Many(); addressFKManyToMany2.setAddressDetail("深圳市罗湖区"); Address_FK_Many_to_Many addressFKManyToMany3 = new Address_FK_Many_to_Many(); addressFKManyToMany3.setAddressDetail("长沙市雨花区"); fkManyToMany1.getAddressFKManyToManieSet().add(addressFKManyToMany1); fkManyToMany1.getAddressFKManyToManieSet().add(addressFKManyToMany2); fkManyToMany2.getAddressFKManyToManieSet().add(addressFKManyToMany2); fkManyToMany2.getAddressFKManyToManieSet().add(addressFKManyToMany3); session.beginTransaction().begin(); //保存Peron第一个对象; session.save(fkManyToMany1); //保存Person第二个对象; session.save(fkManyToMany2); session.beginTransaction().commit(); } }
控制输出入如下:
1,ZhouHaiTao,22
1,深圳市罗湖区
2,北京市朝阳区
2,ZhangSan,23
1,深圳市罗湖区
3,长沙市雨花区