多对多关联

来源:互联网 发布:便携式标签打印机软件 编辑:程序博客网 时间:2024/04/29 13:08
 多对多关联的实现是通过中间表实现的。通过中间表,将这两个表之间的多寻多关联关系转换为它们分别和中间表直接的一对多的关联关系。对于Hibernate来说,并不需要建立专门的中间对象来实现这种转换,但是中间表还是需要建立的。

Hibernate是通过<many-to-many>元素来描述对象之间多对多的关系的

 

Create database many2many;

Use many2many;

CREATE TABLE student(

       Id int(11) NOT NULL auto_increment,

       Name varchar(20) default NULL,

       Age int(11)default Null,

       Specially varchar(20) default NULL,

       PRIMARY KEY (id))ENGINE=InnoDB default charset=gbk;

 

CREATE TABLE stusubmap(

       Id int(11) NOT NULL auto_increment,

       Stuid int(11) default NULL,

       Stuid int(11) default NULL,

       PRIMARY KEY(id)     

)ENGING=InnoDB DEFAULT CHARSET=gbk;

 

 

1:单向关联:建立学生到课程的关联

 

在student.java中添加如下语句:

Private Set subjects=new HashSet();

Public Set getSubjects(){

       Return subjects;

}

Public void setSubjects(Set subjects){

       This.subjects=subjects;

}

在Student.hbm.xml中加入以下语句:

<set name=”subjects” table=”stusubmap” cascade=”save-update”>

       <key column=”stuid”/>

       <many-to-many class=”cn.bsw.hibernate.Subject” column=”subid”/>

</set>

 

2:插入学生和课程

Student stu=new Student();

Stu.setAge(22);

Stu.setName(“tom”);

Stu.setSpecialty(“infomation”);

Set subjects=new HashSet();

Subject subl=new Subject():

Subl.setName(“struts”);

Subjects.add(subl);

 

Subject sub2=new Subject();

Sub2.setName(“hibernate”);

Subjects.add(sub2);

 

Stu.setSubjects(subjects);

Session.save(stu);

 

3:插入学生:

       Student stu=new Student();

       Stu.setAge(22);

Stu.setName(“tom2”);

Stu.setSpecialty(“infomation”);

       Query query=session.createQery(“from subject where name=’struts’”);

       Subject sub=(Subject)query.uniqueResult();

       Stu.getSubjects().add(sub);

 

4:插入学生,若课程不存在则插入课程

5.仅插入选课记录:

Query queryl=session.createQuery(“from Subject where name=’servlet’”);

Subject sub=(Subject)queryl.uniqueResult();

Query quer=session. createQuery(“from Student where id=15”);

Student stu=(Student)query.uniqueResult();

Stu.getSubjects().add(sub);

Session.flush();

 

6:查询课程:

Query query=session.createQuery(“from Student”);

       List list=query.list();

       For(int i=0;i<list.size(); i++){

              System.out.println(“----------------------------------------”)

              Student stud=(Student)list.get(i);

              System.out.println(”stu name: ”+stud.getAge());

              Set subs=stud.getSubjects();

              Iterator it=subs.iterator();

              While(it.hasNext()){

              {

                     Subject sub=(Subject)it..next();

                     System.out.print(“ ”+sub.getName());

              }

}

 

7:删除学生及选课记录:

Query query=session.createQuery(“from Student where id=10”);

       Student stu=(Student)query.uniqueResult();

       Session.delete(stu);

8:仅删除选课记录:

Query queryl=session.creteQuery(“from Subject  where name=’jsp’”);

       Subject sub=(Subject)queryl.uniqueResult();

       Query query=session.createQuery(“from Student where id=16”);

9:双向关联:在单向关联基础上建立课程到学生的关联

在Subject.java中添加如下语句:

Private Set students=new HashSet();

Public Set getStudents(){

       Return students;

}

Public void setStudents(Set students){

       This.students=students;

}

在Subjectt.hbm.xml中加入以下语句

<set name=”students” table=”stusubmap” cascade=”save-update”>

       <key column=”subid”/>

<many-to-many class=”cn.bsw.hibernte.Student” column=”stuid”/>

</set>

 

10:inverse的使用

在多对多关联中可根据实际情况选择由某一方或两方维护对象之间的关联关系,不能将两端均设置为inverse=true;

       Student stu=new Student();

       Stu.setAge(22);

Stu.setName(“tom2”);

Stu.setSpecialty(“infomation”);

       Subject sub=new Subject();

       Sub.setName(“linux”);

       Stu.getSubjects().add(sub);

       Stu.getStudents().add(stu);

       Session.save(stu):