经典的多对多例子,一个学生可以选n门课,一门课可以被n个人选。
所有代码使用上一篇中的。
在Person类中加入:
public Set getCourses() {
return courses;
}
private void setCourses(Set courses) {
this.courses = courses;
}
在Person.hbm.xml文件<property name="sex"/>后加入:
<set name="courses" table="PERSON_COURSE">
<key column="PERSON_ID"/>
<many-to-many column="COURSE_ID" class="examples.Course"/>
</set>
这个set对应Person类中的courses,我们知道多对多要从两个表变成三个表,talb
e指明中间关系表的名称,key指明中间表中Person的标识字段,many-to-many表示
是一个多对多关系
新建一个Course类:
package examples;
import java.util.HashSet;
import java.util.Set;
public class Course {
private Long id;
private String name;
private Set persons = new HashSet();
public Set getPersons() {
return persons;
}
private void setPersons(Set persons) {
this.persons = persons;
}
public Long getId() {
return id;
}
private void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
新建相应的Course.hbm.xml文件:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="examples.Course">
<id name="id">
<generator class="increment"/>
</id>
<property name="name"/>
<set name="persons" inverse="true" table="PERSON_COURSE">
<key column="COURSE_ID"/>
<many-to-many column="PERSON_ID" class="examples.Person"/>
</set>
</class>
</hibernate-mapping>
需要说明的是inverse=true的属性,多对多有一方是主控方,这里应该是Person类
,所以Course类加上inverse=true
在hibernate.cfg.xml中加入
<mapping resource="examples/Course.hbm.xml"/>
在Test.java中加入:
Course c1 = new Course();
Course c2 = new Course();
Course c3 = new Course();
c1.setName("c1");
c2.setName("c2");
c3.setName("c3");
p1.getCourses().add(c2);
p2.getCourses().add(c3);
p3.getCourses().add(c1);
session.save(c1);
session.save(c2);
session.save(c3);
修改Test.java的fetchData方法中的打印语句为:
System.out.println("" + p.getId() +" :" + p.getName() + " :" + p.getSex(
) + " :" + ((Course)p.getCourses().iterator().next()).getName());
运行,应该有结果:
1 :p1 :M :c2
3 :p3 :M :c1
相应的sql语句为:
11:21:34,921 DEBUG SchemaUpdate:142 - create table Course (id bigint not
null, name varchar(255), primary key (id))
11:21:34,921 DEBUG SchemaUpdate:142 - create table PERSON_COURSE (PERSON
_ID bigint not null, COURSE_ID bigint not null, primary key (PERSON_ID,
COURSE_ID))
11:21:34,921 DEBUG SchemaUpdate:142 - create table Person (id bigint not
null, name varchar(255), sex char(1), primary key (id))
11:21:34,937 DEBUG SchemaUpdate:142 - alter table PERSON_COURSE add cons
traint FKE448459882F6FF foreign key (PERSON_ID) references Person
11:21:34,937 DEBUG SchemaUpdate:142 - alter table PERSON_COURSE add cons
traint FKE44845A766793F foreign key (COURSE_ID) references Course
11:21:34,937 INFO SchemaUpdate:153 - schema update complete
11:21:34,937 INFO SessionFactoryImpl:379 - Checking 0 named queries
Hibernate: insert into Person (name, sex, id) values (?, ?, ?)
Hibernate: insert into Person (name, sex, id) values (?, ?, ?)
Hibernate: insert into Person (name, sex, id) values (?, ?, ?)
Hibernate: insert into Course (name, id) values (?, ?)
Hibernate: insert into Course (name, id) values (?, ?)
Hibernate: insert into Course (name, id) values (?, ?)
Hibernate: insert into PERSON_COURSE (PERSON_ID, COURSE_ID) values (?, ?
)
Hibernate: insert into PERSON_COURSE (PERSON_ID, COURSE_ID) values (?, ?
)
Hibernate: insert into PERSON_COURSE (PERSON_ID, COURSE_ID) values (?, ?
)
Hibernate: select person0_.id as col_0_0_ from Person person0_ where per
son0_.sex=?
Hibernate: select person0_.id as id0_, person0_.name as name0_0_, person
0_.sex as sex0_0_ from Person person0_ where person0_.id=?
Hibernate: select person0_.id as id0_, person0_.name as name0_0_, person
0_.sex as sex0_0_ from Person person0_ where person0_.id=?
Hibernate: select courses0_.PERSON_ID as PERSON1_1_, courses0_.COURSE_ID
as COURSE2_1_, course1_.id as id0_, course1_.name as name2_0_ from PERS
ON_COURSE courses0_ inner join Course course1_ on courses0_.COURSE_ID=co
urse1_.id where courses0_.PERSON_ID=?
Hibernate: select courses0_.PERSON_ID as PERSON1_1_, courses0_.COURSE_ID
as COURSE2_1_, course1_.id as id0_, course1_.name as name2_0_ from PERS
ON_COURSE courses0_ inner join Course course1_ on courses0_.COURSE_ID=co
urse1_.id where courses0_.PERSON_ID=?