一对多关系映射

  两张表之间如果存在外键,就会出现一对多关系(外键不能同时是主键)
  如果想使用pojo类来表示一对多关系,这里以省份和城市为例:
  省份类中应该包含多个城市的对象,一般默认使用Set集合表示某个省份下多个城市
  城市类中应该包含一个省份的对象。
  如果在表中存在外键,且使用MyEclipse一起选择两张表生成映射,则会自动生成出一对多关系。
  先建立省份和城市表。
  CREATE TABLE province (
  id number(8) primary key ,
  title varchar2(50) not null
  );
  INSERT INTO province VALUES (1,'江苏');
  INSERT INTO province VALUES (2,'广东');
  INSERT INTO province VALUES (3,'河南');
  CREATE TABLE city (
  id number(8) primary key ,
  title varchar2(50) not null,
  province_id number(8) ,
  foreign key (province_id) references province (id) on delete cascade
  );
  INSERT INTO city VALUES (1,'南通¨',1);
  INSERT INTO city VALUES (2,'南京',1);
  INSERT INTO city VALUES (3,'苏州',1);
  INSERT INTO city VALUES (4,'广州',2);
  INSERT INTO city VALUES (5,'深圳',2);
  INSERT INTO city VALUES (6,'洛阳',3);
  INSERT INTO city VALUES (7,'南阳',3);
  COMMIT;
  生成映射:
  public class Province implements java.io.Serializable {
  private Integer id;
  private String title;
  private Set cities = new HashSet(0);
  public class City implements java.io.Serializable {
  private Integer id;
  private Province province;
  private String title;
  通过映射文件描述了这种一对多关系。
  <?xml version="1.0" encoding="utf-8"?>
  <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  <hibernate-mapping>
  <class name="org.liky.primary.pojo.Province" table="PROVINCE" schema="SUNXUN">
  <id name="id" type="java.lang.Integer">
  <column name="ID" precision="8" scale="0" />
  <generator class="increment" />
  </id>
  <property name="title" type="java.lang.String">
  <column name="TITLE" length="50" not-null="true" />
  </property>
  <!--
  在Province中存在一个名称为cities的Set集合
  -->
  <set name="cities" inverse="true">
  <!--
  关联外键是PROVINCE_ID
  -->
  <key>
  <column name="PROVINCE_ID" precision="8" scale="0" />
  </key>
  <!--
  表示当前类(Province)与City类存在一对多关系
  -->
  <one-to-many class="org.liky.primary.pojo.City" />
  </set>
  </class>
  </hibernate-mapping>
  <?xml version="1.0" encoding="utf-8"?>
  <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  <hibernate-mapping>
  <class name="org.liky.primary.pojo.City" table="CITY" schema="SUNXUN">
  <id name="id" type="java.lang.Integer">
  <column name="ID" precision="8" scale="0" />
  <generator class="increment" />
  </id>
  <!--
  当前类(city)与Province存在多对一关系,关联的对象是province,表中关联的字段是province_id
  -->
  <many-to-one name="province" class="org.liky.primary.pojo.Province" fetch="select">
  <column name="PROVINCE_ID" precision="8" scale="0" />
  </many-to-one>
  <property name="title" type="java.lang.String">
  <column name="TITLE" length="50" not-null="true" />
  </property>
  </class>
  </hibernate-mapping>
  Inverse="true"表示:关联关系反转,也就是关联关系由对方进行维护。
  关联关系在这里指的是外键字段province_id,该字段是city表的,也就是说修改city表,才可以修改这个字段,也就可以说city表维护着关联关系。对于Province来说,对方就是city,也就是说关联关系由对方(city)来维护雅思答案 www.jamo123.com
  public static void main(String[] args) {
  City c = (City) HibernateSessionFactory.getSession()。get(City.class, 5);
  System.out.println(c.getTitle());
  System.out.println(c.getProvince()。getTitle());
  }
  通过这段测试,可以看出fetch的作用
  使用get或load方法按主键查询时,如果fetch="select",是懒汉式加载,先查询出城市信息,当用到省份信息时,再查询省份。
  Fetch="join",则是一次关联查询将省份和城市一起取得托福答案 www.yztrans.com
  使用了一对多关系后,Hibernate可以自动根据关联关系,将关联数据取得。
  例如:
  查询省份,可以自动将所有城市也列表显示出来。
  String hql = "FROM Province";
  List<Province> allP = HibernateSessionFactory.getSession()。createQuery(
  hql)。list();
  Iterator<Province> iter = allP.iterator();
  while (iter.hasNext()) {
  Province p = iter.next();
  System.out.println(p.getTitle());
  Iterator<City> iter2 = p.getCities()。iterator();
  while(iter2.hasNext()) {
  City c = iter2.next();
  System.out.println(" |- " + c.getTitle());
  }
  }

posted on 2014-04-24 22:49  haosola  阅读(296)  评论(0编辑  收藏  举报

toeflacttoeflieltstoefltoeflact