hibernate 之 關聯映射的雙向一對多關聯

來源:互聯網
上載者:User

標籤:stc   ber   hiberna   print   hibernate   void   分享   exp   exception   

1. 考慮學生表 和 教師表,表結構和單向的表結構一樣。

2. 類結構:Teacher.java

public class Teacher {    private int id;    private String name;    private Set<Student> students = new HashSet<Student>();    public Teacher() {    }    public Teacher(String name) {        super();        this.name = name;    }    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 Set<Student> getStudents() {        return students;    }    public void setStudents(Set<Student> students) {        this.students = students;    }}

Student.java

public class Student {    private int id;    private String name;    private String sex;    private Teacher teacher;    public Student() {    }    public Student(String name, String sex) {        super();        this.name = name;        this.sex = sex;    }    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 String getSex() {        return sex;    }    public void setSex(String sex) {        this.sex = sex;    }    public Teacher getTeacher() {        return teacher;    }    public void setTeacher(Teacher teacher) {        this.teacher = teacher;    }}

3.  對應檔:Teacher.hbm.xml :

<hibernate-mapping>    <class name="cn.wh.vo.Teacher" table="t_teacher">        <id name="id">            <generator class="native"></generator>        </id>        <property name="name"/>        <set name="students" inverse="true">            <key column="tid"></key>            <one-to-many class="cn.wh.vo.Student"/>        </set>    </class></hibernate-mapping>

Student.hbm.xml:

<hibernate-mapping>    <class name="cn.wh.vo.Student" table="t_student">        <id name="id">            <generator class="native"></generator>        </id>        <property name="name"/>        <property name="sex"/>        <!-- 配置多對一的關聯映射            name配置的是  一的一端在多的一端的屬性名稱            column 配置的是 一的一端在多的一端表中的外鍵         -->        <many-to-one name="teacher" column="tid"/>    </class></hibernate-mapping>

4. 測試:

public class HibernateTest {    private Session session=null;    @Before    public void setUp(){        session = HibernateUtil.getSession();    }    @After    public void tearDown(){        HibernateUtil.close();    }    @Test    public void testCreateDB(){        Configuration cfg = new Configuration().configure();        SchemaExport export = new SchemaExport(cfg);        //第一個參數  是否列印  資料庫指令碼        //第二個參數 是否將指令碼匯入資料庫執行        export.create(true, true);    }    @Test    public void testInit(){        Transaction tx = null;        try {            tx = session.beginTransaction();            Teacher t1 = new Teacher("nico");            Teacher t2 = new Teacher("michile");            Student s1 = new Student("liming","男");            s1.setTeacher(t1);            Student s2 = new Student("hanmei","女");            s2.setTeacher(t1);            Student s3 = new Student("jim","男");            s3.setTeacher(t1);                        session.save(t1);            session.save(t2);            session.save(s1);            session.save(s2);            session.save(s3);            tx.commit();                    } catch (Exception e) {            if(tx!=null)                tx.rollback();        }    }    @Test    public void testGet(){        Student stu = (Student)session.get(Student.class, 1);        System.out.println(stu.getName()+"---"+stu.getSex()+"----"+stu.getTeacher().getName());        System.out.println("----------------------------");        Teacher t = (Teacher)session.get(Teacher.class, 1);        System.out.println("teacherName==="+t.getName());        for(Student s:t.getStudents()){            System.out.println(s.getName()+"----"+s.getSex());        }            }    }

 

hibernate 之 關聯映射的雙向一對多關聯

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.