首页 > 代码库 > hibernate 之 关联映射的双向一对多关联
hibernate 之 关联映射的双向一对多关联
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个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。