想飛就別怕摔

          大爺?shù)牟M罵人

          Hibernate學(xué)習(xí)(八)---【多對(duì)多】

          舉一個(gè)簡(jiǎn)單的例子:
          Teacher.java
          package zzn.hibernate.model;

          import java.util.Set;

          public class Teacher {
              
          private int id;
              
          private String name;
              
          private int age;
              
          private Set<Student> student;

              
          public Set<Student> getStudent() {
                  
          return student;
              }

              
          public void setStudent(Set<Student> student) {
                  
          this.student = student;
              }

              
          public int getId() {
                  
          return id;
              }

              
          public void setId(int id) {
                  
          this.id = id;
              }

              
          public String getName() {
                  
          return name;
              }

              
          public void setName(String name) {
                  
          this.name = name;
              }

              
          public int getAge() {
                  
          return age;
              }

              
          public void setAge(int age) {
                  
          this.age = age;
              }

          }
          Student.java
          package zzn.hibernate.model;

          import java.util.Set;

          public class Student {
              
          private int id;
              
          private String name;
              
          private int age;
              
          private Set<Teacher> teacher;
              
              
          public Set<Teacher> getTeacher() {
                  
          return teacher;
              }

              
          public void setTeacher(Set<Teacher> teacher) {
                  
          this.teacher = teacher;
              }

              
          public int getId() {
                  
          return id;
              }

              
          public void setId(int id) {
                  
          this.id = id;
              }

              
          public String getName() {
                  
          return name;
              }

              
          public void setName(String name) {
                  
          this.name = name;
              }

              
          public int getAge() {
                  
          return age;
              }

              
          public void setAge(int age) {
                  
          this.age = age;
              }

          }
          Teacher.hbm.xml
          <?xml version="1.0" encoding='gb2312'?>  
          <!DOCTYPE hibernate-mapping PUBLIC  
                  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
                  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
          >  
            
          <hibernate-mapping package="zzn.hibernate.model">  
              
          <class name="Teacher" table="teacher" >
                  
          <id name="id" column="id">
                      
          <generator class="identity" />
                  
          </id>
                  
                  
          <property name="name" />
                  
          <property name="age" />
                  
          <set name="Student" table="student_teacher">
                      
          <key column="teacher_id" />
                      
          <many-to-many class="Student" column="student_id" />
                  
          </set>
              
          </class>
          </hibernate-mapping>
          Student.hbm.xml
          <?xml version="1.0" encoding='gb2312'?>  
          <!DOCTYPE hibernate-mapping PUBLIC  
                  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
                  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
          >  
            
          <hibernate-mapping package="zzn.hibernate.model">  
              
          <class name="Student" table="student" >
                  
          <id name="id" column="id">
                      
          <generator class="identity" />
                  
          </id>
                  
                  
          <property name="name" />
                  
          <property name="age" />
                  
                  
          <set name="Teacher" table="student_teacher">
                      
          <key column="student_id" />
                      
          <many-to-many class="Teacher" column="teacher_id" />
                  
          </set>
              
          </class>
          </hibernate-mapping>
          hibernate-config.xml
          <?xml version='1.0' encoding='gb2312'?>
          <!DOCTYPE hibernate-configuration PUBLIC
                    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"
          >

          <hibernate-configuration>
              
          <session-factory>
                  
          <property name="show_sql">true</property>
                  
          <property name="hibernate.hbm2ddl.auto">update</property>
                  
          <property name="connection.username">sa</property>
                  
          <property name="connection.password"></property>
                  
          <property name="connection.url">jdbc:jtds:sqlserver://localhost:1433;databasename=hibernate_test</property>
                  
          <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
                  
          <property name="myeclipse.connection.profile">SQL2005</property>
                  
          <property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
              
                  
          <mapping resource="zzn/hibernate/mappings/Teacher.hbm.xml"/>
                  
          <mapping resource="zzn/hibernate/mappings/Student.hbm.xml"/>
                  
              
          </session-factory>

          </hibernate-configuration>
          ManyToManyTest.java
          package zzn.hibernate.test;

          import java.util.ArrayList;
          import java.util.HashSet;
          import java.util.List;
          import java.util.Set;

          import org.hibernate.Session;
          import org.hibernate.SessionFactory;
          import org.hibernate.Transaction;
          import org.hibernate.cfg.Configuration;

          import zzn.hibernate.base.HibernateUtil;
          import zzn.hibernate.model.Student;
          import zzn.hibernate.model.Teacher;

          public class ManyToManyTest {
              
          public static void main(String[] args) {
                  add();
                  get(
          1);
              }

              
          public static void add() {
                  Configuration configuration 
          = null;
                  SessionFactory sessionFactory 
          = null;
                  Session session 
          = null;
                  Transaction transaction 
          = null;

                  Student student 
          = new Student();
                  Teacher teacher 
          = new Teacher();

                  
          try {
                      configuration 
          = new Configuration();
                      sessionFactory 
          = configuration.configure().buildSessionFactory();
                      session 
          = sessionFactory.openSession();
                      transaction 
          = session.beginTransaction();
                      
                      Set
          <Student> stu = new HashSet<Student>();
                      student.setName(
          "zhaozhaonan");
                      student.setAge(
          25);
                      stu.add(student);

                      teacher.setName(
          "zhangxiaolan");
                      teacher.setAge(
          25);
                      
                      teacher.setStudent(stu);//建立關(guān)聯(lián)
                      session.save(student);
                      session.save(teacher);
                  } 
          finally {
                      
          if (session != null) {
                          transaction.commit();
                          session.close();
                      }
                  }
              }

              @SuppressWarnings(
          "unchecked")
              
          public static void get(int id) {
                  Session session 
          = null;
                  Transaction transaction 
          = null;
                  
          try {
                      session 
          = HibernateUtil.getSession();
                      transaction 
          = session.beginTransaction();
                      Student student 
          = (Student)session.get(Student.class, id);
                      List list 
          = new ArrayList (student.getTeacher());
                      
          for(int i=0;i<list.size();i++){
                          Teacher teacher 
          = (Teacher)list.get(i);
                          System.out.println(teacher.getName());
                      }
                  } 
          finally {
                      
          if (session != null) {
                          transaction.commit();
                          session.close();
                      }
                  }
              }
          }

          posted on 2009-06-28 13:36 生命的綻放 閱讀(317) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): Hibernate

          <2009年6月>
          31123456
          78910111213
          14151617181920
          21222324252627
          2829301234
          567891011

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(5)

          隨筆分類(lèi)(94)

          隨筆檔案(93)

          文章分類(lèi)(5)

          文章檔案(5)

          相冊(cè)

          JAVA之橋

          SQL之音

          兄弟之窗

          常用工具下載

          積分與排名

          最新評(píng)論

          閱讀排行榜

          主站蜘蛛池模板: 阳高县| 闽侯县| 大埔县| 团风县| 临洮县| 万盛区| 大安市| 林周县| 朝阳区| 施甸县| 上杭县| 襄城县| 大荔县| 大厂| 瓦房店市| 阿瓦提县| 双鸭山市| 辉县市| 白朗县| 通道| 秦安县| 阿瓦提县| 盐津县| 汕头市| 龙岩市| 临漳县| 宁安市| 中宁县| 梅州市| 高碑店市| 青神县| 沁阳市| 康定县| 隆尧县| 原阳县| 越西县| 屏东县| 仪征市| 中牟县| 湘潭市| 佛冈县|