标签:style class blog c code java
上篇文章讨论了双向关联的一对一映射,用了两个章节,主要是从主键和外键两种关联映射展开详细讨论,双向关联的映射需要在两个映射文件中分别添加相互的对应关系,斌刚在相应的类中添加对应的关联类的属性,这样在一端加载时才能加载到另一端的对象。关联中常用的主要有多对一、一对一、一对多和多对多,我们已经讨论了两种映射关系,接下来将会讨论一对多的关系。
<pre name="code" class="java">package com.src.hibernate; import java.util.Set; public class Classes { private int id; 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; } private String name; //Set支持延迟加载 private Set students; public Set getStudents() { return students; } public void setStudents(Set students) { this.students = students; } }Classes对象中使用了set属性,但是只是说明了延迟加载的属性,并没有为属性配置对应的对象,属性的对象是要在映射文件中来配置的,需要添加set标签,并在set标签中添加<one-to-many>标签,具体如下代码:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.hibernate.Classes" table="t_classes"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <set name="students"> <key column="classesid"></key> <one-to-many class="com.hibernate.Student"></one-to-many> </set> </class> </hibernate-mapping>
alter table t_student drop foreign key FK4B9075705E0AFEFE drop table if exists t_classes drop table if exists t_student create table t_classes (id integer not null auto_increment, name varchar(255), primary key (id)) create table t_student (id integer not null auto_increment, name varchar(255), classesid integer, primary key (id)) alter table t_student add index FK4B9075705E0AFEFE (classesid), add constraint FK4B9075705E0AFEFE foreign key (classesid) references t_classes (id)
public void testSave2(){ Session session=null; try{ session=HibernateUtils.getSession(); session.beginTransaction(); Student student1=new Student(); student1.setName("zhangsan"); session.save(student1); Student student2=new Student(); student2.setName("lisi"); session.save(student2); Classes classes=new Classes(); classes.setName("ClassOne"); Set students=new HashSet(); students.add(student1); students.add(student2); classes.setStudents(students); //可以成功保存数据 //但是会发出多余的update语句来维持关系,因为是一对多的原因 session.save(classes); session.getTransaction().commit(); }catch(Exception e){ e.printStackTrace(); session.getTransaction().rollback(); }finally{ HibernateUtils.closeSession(session); } }
package com.test.hibernate; import java.util.Iterator; import java.util.Set; import com.src.hibernate.*; import junit.framework.TestCase; import org.hibernate.Session; public class One2ManyTest extends TestCase { public void testLoad1(){ Session session=null; try{ session=HibernateUtils.getSession(); session.beginTransaction(); //获取主键为5的班级信息 Classes classes=(Classes)session.load(Classes.class,5); //打印班级信息 System.out.println("classes.name="+classes.getName()); //设置学生集合,通过班级加载学生集合 Set students=classes.getStudents(); //迭代集合,打印集合中学生的信息 for(Iterator iter=students.iterator();iter.hasNext();){ Student student=(Student)iter.next(); System.out.println("student.name="+student.getName()); } session.getTransaction().commit(); }catch(Exception e){ e.printStackTrace(); session.getTransaction().rollback(); }finally{ HibernateUtils.closeSession(session); } } }
Hibernate: select classes0_.id as id1_0_, classes0_.name as name1_0_ from t_classes classes0_ where classes0_.id=? classes.name=ClassOne Hibernate: select students0_.classesid as classesid1_, students0_.id as id1_, students0_.id as id0_0_, students0_.name as name0_0_ from t_student students0_ where students0_.classesid=? student.name=lisi student.name=zhangsan
【Hibernate步步为营】--单向关联之一对多映射,布布扣,bubuko.com
标签:style class blog c code java
原文地址:http://blog.csdn.net/zhang_xinxiu/article/details/26761713