按着我们的总结行进计划,接下来,就是有关于多对多映射的总结了。
我们来举个例子啊,很长时间以来,房价暴涨不落,但是还有很多人拥有很多套房产,假如说,一个富豪拥有九套房产,家里人么准去住哪一套,我们就以说:
对于富豪家人来说:一个人可以拥有很多住址Address;对于房子来说:一套房子可以让很多人住。
1、多对多单向关联映射:
Po对象:Person.Java:
- public class Person
- {
- private int id;
- private String name;
- private Set<Address> address;
- //getter\setter
- }
Address.java
- public class Address
- {
- private int id;
- private String name;
- }
映射文件:Person.hbm.xml
- <hibernate-mapping package="org.hibernate.test">
- <class name="com.ssh.hibernate.Person" table="t_person">
- <id name="id">
- <generator class="native"/>
- </id>
- <set name="addres" table="t_personAddress">
- <key column="personid"/>
- <many-to-many column="addressId" class="com.ssh.hibernate.Address"/>
- </set>
- </class>
- </hibernate-mapping>
Address.hbm.xml:
- <hibernate-mapping package="org.hibernate.test">
- <class name="com.ssh.hibernate.Address" table="t_address">
- <id name="id">
- <generator class="native" />
- </id>
- <property name="name" />
- </class>
- </hibernate-mapping>
运行程序,生成表语句:
- create table Person ( id bigint not null, name varchar(255), primary key(id) )
- create table PersonAddress ( personid bigint not null, addressid bigint not null, primary key (personid, addressid) )
- create table Address ( id bigint not null,name varchar(255), primary key(id) )
测试:
- session.beginTransaction();
- Address address1=new Address();
- address1.setName("<span margin: 0px; padding: 0px; border: currentColor; color: black; background-color: inherit;">font-family: Arial, Helvetica, sans-serif;">唐人街5号</span>");
- session.save(address1);
- Address address2=new Address();
- address2.setName("北京帽儿胡同12号");
- session.save(address2);
- Address address3=new Address();
- address3.setName("南京花雨石大街10号");
- session.save(course3);
- Address address4=new Address();
- address4.setName("长安大街11号");
- session.save(address4);
- Person person1=new Person();
- Set addres1=new HashSet();
- addres1.add(address1);
- addres1.add(address2);
- person1.setAddresss(addres1);
- person1.setName("赵钱孙");
- session.save(person1);
- Person person2=new Person();
- Set addres2=new HashSet();
- addres2.add(address2);
- addres2.add(address4);
- person2.setAddresss(addres2);
- person2.setName("甲乙丙");
- session.save(person2);
- session.getTransaction().commit();
执行完成后,进行查询测试:
- session.beginTransaction();
- Person person=(Person)session.load(person.class, 1);
- System.out.println(person.getName());
- for(Address s:person.getaddres()){
- System.out.println(s.getName());
- }
- session.getTransaction().commit();
执行查询结果:
- 赵钱孙
- 唐人街5号
- 北京帽儿胡同12号
2、多对多双向关联映射:
Po对象:Person.java:
- public class Person
- {
- private int id;
- private String name;
- private Set<Address> address;
- //getter\setter
- }
Address.java
- public class Address
- {
- private int id;
- private String name;
- private Set<Person> person;
- }
配置文件:Person.hbm.xml
- <class name="com.ssh.hibernate.Person" talbe="t_person">
- <id name="id" column="id">
- <generator class="native"/>
- </id>
- <set name="addres" table="t_personAddress">
- <key column="personId"/>
- <many-to-many column="addressId" class="com.ssh.hibernate.Address"/>
- </set>
- </class>
Address.hbm.xml
- <class name="com.ssh.hibernate.Address" table="t_address">
- <id name="id" column="id">
- <generator class="native"/>
- </id>
- <set name="people" inverse="true" table="t_personAddress">
- <key column="addressId"/>
- <many-to-many column="personId" class="com.ssh.hibernate.Person"/>
- </set>
- </class>
这就是双向的了,通过住户可以知道他的所有房产,也可以通过房产知道它的所有的住户。其中值得一说的是<set>的<inverse="true">,还记得咱们的IOC容器的全称吗?Inverse Of Control,控制反转,这里指的也是反转,在多对多关联中,如果设置了inverse="true"就表示本方不进行关联的维护,由另一方进行关联的维护。就那上面实力来说,因为设置了<inverse="true">,所以即使执行:
- Set<Address> adres=new HashSet<Address>();
- adres.add(new Address("北京胡同"));
- adres.add(new Address("南京胡同"));
- person.setAdres(adres);
- session.save(person);
也只会想t_person表中插入数据,而不会向t_personAddress表中插入数据,如果想要同时插入数据,只需要将inserve设置为false;在多对多双向关联映射中,任何一方设置inserve=true都可以,没有特殊限定,因为两方都一样。