和风细雨

世上本无难事,心以为难,斯乃真难。苟不存一难之见于心,则运用之术自出。

持久化对象级联关系

类之间关联的Hibernate表现

在Java程序中,类之间存在多种包含关系,典型的三种关联关系有:一个类拥有另一个类的成员,一个类拥有另一个类的集合的成员;两个类相互拥有对象的集合的成员.在Hibernate中,我们可以使用映射文件中的many-to-one, one-to-many, many-to-many来实现它们.这样的关系在Hibernate中简称为多对一,一对多和多对多.

多对一的类代码

事件与地点是典型的多对一关系,多个事件可以在一个地点发生(时间不同),一个地点可发生多个事件.它们的对应关系是(多)事件对(一)地点.
两个类的代码如右:
public class Event{
  private String id;
  private String name; 
  private Location location;
}

public class Location{
  private String id;
  private String name;
}

多对一的映射文件

<?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 package="org.hibernate.auction">
  <class name="com.sitinspring.domain.Event"
    table="Event_TB">
    <id name="id" column="ID" >
      <generator class="uuid.hex"/>
    </id>
    <property name="name" column="name" />
    <many-to-one name="location" column="locationId" class="com.sitinspring.domain.Location"/>
  </class>
</hibernate-mapping>


<?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 package="org.hibernate.auction">
  <class name="com.sitinspring.domain.Location"
    table="Location_TB">
    <id name="id" column="ID" >
      <generator class="uuid.hex"/>
    </id>
    <property name="name" column="name" />
  </class>
</hibernate-mapping>

多对一的表数据


一对多的类代码

如果一个用户有多个权限,那么User类和Privilege类就构成了一对多的关系,User类将包含一个Privilege类的集合.

public class User{
  private String id;
  private String name;
  private Set<Privilege> privileges=new LinkedHashSet<Privilege>();
}

public class Privilege{
  private String id;
  private String userId;
  private int privilegeLevel;
}


一对多的映射文件

<?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 package="org.hibernate.auction">
  <class name="com.sitinspring.domain.User"
    table="User_TB">
    <id name="id" column="ID" >
      <generator class="uuid.hex"/>
    </id>
    <property name="name" column="name" />
    <set name="privileges">
      <key column="userId"/>
      <one-to-many class="com.sitinspring.domain.Privilege"/>
    </set>
  </class>
</hibernate-mapping>


<?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 package="org.hibernate.auction">
  <class name="com.sitinspring.domain.Privilege"
    table="Privilege_TB">
    <id name="id" column="ID" >
      <generator class="uuid.hex"/>
    </id>
    <property name="userId" column="userId" />
    <property name="privilegeLevel" column="privilegeLevel" />
  </class>
</hibernate-mapping>


一对多的表数据



多对多

多对多关系 是指两个类相互拥有对方的集合,如文章和标签两个类,一篇文章可能有多个标签,一个标签可能对应多篇文章.要实现这种关系需要一个中间表的辅助.

类代码如右:

public class Article{
    private String id;
    private String name;
   
    private Set<Tag> tags = new HashSet<Tag>();
}

public class Tag{
    private String id;
    private String name;
   
    private Set<Article> articles = new HashSet<Article>();
}

多对多的映射文件

<?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 package="org.hibernate.auction">
  <class name="com.sitinspring.domain.Article" table="ARTICLE_TB">
    <id name="id" column="ID" >
      <generator class="uuid.hex"/>
    </id>
    <property name="name" column="NAME" />

    <set name="tags" table="ARTICLETAG_TB"  cascade="all" lazy="false">
      <key column="ARTICLEID" />
      <many-to-many column="TAGID" class="com.sitinspring.domain.Tag" />
    </set>
  </class>
</hibernate-mapping>


<?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 package="org.hibernate.auction">
  <class name="com.sitinspring.domain.Tag" table="TAG_TB">
    <id name="id" column="ID" >
      <generator class="uuid.hex"/>
    </id>
    <property name="name"  column="NAME" />
   
    <set name="articles" table="ARTICLETAG_TB" cascade="all" lazy="false">
      <key column="TAGID" />
      <many-to-many column="ARTICLEID" class="com.sitinspring.domain.Article" />
    </set>
  </class>
</hibernate-mapping>


多对多的表数据

 

源码下载:
http://www.blogjava.net/Files/junglesong/HibernateMapping20080430203526.rar

posted on 2008-04-30 20:33 和风细雨 阅读(796) 评论(0)  编辑  收藏 所属分类: Hibernate


只有注册用户登录后才能发表评论。


网站导航: