按着咱們的總結行進計劃,接下來,就是有關於多對多映射的總結了。html
咱們來舉個例子啊,很長時間以來,房價暴漲不落,可是還有不少人擁有不少套房產,假如說,一個富豪擁有九套房產,家裏人麼準去住哪一套,咱們就以說:java
對於富豪家人來講:一我的能夠擁有不少住址Address;對於房子來講:一套房子可讓不少人住。web
一、多對多單向關聯映射:sql
Po對象:Person.Java:session
- public class Person
- {
- private int id;
- private String name;
- private Set<Address> address;
-
- }
Address.javaapp
- public class Address
- {
- private int id;
- private String name;
- }
映射文件:Person.hbm.xmlssh
- <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:oop
- <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) )
測試:this
- 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();
執行查詢結果:
二、多對多雙向關聯映射:
Po對象:Person.java:
- public class Person
- {
- private int id;
- private String name;
- private Set<Address> address;
-
- }
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均可以,沒有特殊限定,由於兩方都同樣。