码迷,mamicode.com
首页 > Web开发 > 详细

hibernate 之 关联映射的双向一对多关联

时间:2017-04-20 10:40:28      阅读:235      评论:0      收藏:0      [点我收藏+]

标签: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 之 关联映射的双向一对多关联

标签:stc   ber   hiberna   print   hibernate   void   分享   exp   exception   

原文地址:http://www.cnblogs.com/forever2h/p/6736913.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!