hibernate实体映射配置:多对多、一对多、多对一、特殊的多对一

一、本设计中涉及到三个实体类,之间的关联关系如图所示。


说明:

      部门与用户:一对多

       部门与部门:(自关联)当前部门存在一个上级部门(parent),存在多个下级部门(children)

       用户与角色(岗位):多对多

二、实体类及其映射文件如下。

Department

package cn.itcast.oa.domain;

import java.util.HashSet;
import java.util.Set;

/**
 * 部门
 * 
 * @author tyg
 * 
 */
public class Department {
	private Long id;
	//部门用户
	private Set<User> users = new HashSet<User>();
	//上级部门
	private Department parent;
	//下级部门
	private Set<Department> children = new HashSet<Department>();

	private String name;
	private String description;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Set<User> getUsers() {
		return users;
	}

	public void setUsers(Set<User> users) {
		this.users = users;
	}

	public Department getParent() {
		return parent;
	}

	public void setParent(Department parent) {
		this.parent = parent;
	}

	public Set<Department> getChildren() {
		return children;
	}

	public void setChildren(Set<Department> children) {
		this.children = children;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

}
Department.hbm.xml映射文件

<?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="cn.itcast.oa.domain">

	<class name="Department" table="itcast_department">
		<id name="id">
			<generator class="native" />
		</id>
		<property name="name" />
		<property name="description" />


		<!-- users属性,本类与User的一对多,外键存在于多的一方,即User中的department属性-->
		<set name="users">
			<!--User的deparment属性的id,作为外键-->
			<key column="departmentId"></key>
			<!--该users属性所属类型-->
			<one-to-many class="User" />
		</set>


		<!-- parent属性,本类与Department(上级)的多对一
		     name:属性名称
		     class:属性所属类型
		     column:外键
		 -->
		<many-to-one name="parent" class="Department" column="parentId"></many-to-one>


		<!-- children属性,本类与Department(下级)的一对多
		     name:属性名
		     column:外键
		     class:属性所属类型
		 -->
		<set name="children">
			<key column="parentId"></key>
			<one-to-many class="Department" />
		</set>


	</class>

</hibernate-mapping>

User实体

package cn.itcast.oa.domain;

import java.util.HashSet;
import java.util.Set;

/**
 * 用户
 * @author tyg
 * 
 */
public class User {
	private Long id;
	//用户所属部门
	private Department department;
	//用户所属角色、岗位
	private Set<Role> roles = new HashSet<Role>();

	private String loginName; // 登录名
	private String password; // 密码
	private String name; // 真实姓名
	private String gender; // 性别
	private String phoneNumber; // 电话号码
	private String email; // 电子邮件
	private String description; // 说明

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public Set<Role> getRoles() {
		return roles;
	}

	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getPhoneNumber() {
		return phoneNumber;
	}

	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

}

User.hbm.xml映射文件

<?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="cn.itcast.oa.domain">

	<class name="User" table="itcast_user">
		<id name="id">
            <generator class="native"/>
		</id>
		<property name="loginName" />
		<property name="password" />
		<property name="name" />
		<property name="gender" />
		<property name="phoneNumber" />
		<property name="email" />
		<property name="description" />
		
		
		<!-- department属性,本类与Department的多对一 -->
		<many-to-one name="department" class="Department" column="departmentId"></many-to-one>
		
		
		<!-- roles属性,本类与Role的多对多,多对多关系需要通过中间表维系
		     name:属性名
		     table:中间表


		 -->
		<set name="roles" table="itcast_user_role">
			<!--本类在Role中的外键-->
			<key column="userId"></key>
			<!--
			     class:roles所属类型
			     column:Role在本类中的外键
		    -->
			<many-to-many class="Role" column="roleId"></many-to-many>
		</set>
		
		
	</class>
	
</hibernate-mapping>

Role实体

package cn.itcast.oa.domain;

import java.util.HashSet;
import java.util.Set;

/**
 * 岗位、角色
 * 
 * @author tyg
 * 
 */
public class Role {
	private Long id;
	//名称
	private String name;
	//描述
	private String description;
	//用户
	private Set<User> users = new HashSet<User>();

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Set<User> getUsers() {
		return users;
	}

	public void setUsers(Set<User> users) {
		this.users = users;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

}
Role.hbm.xml映射文件

<?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="cn.itcast.oa.domain">

	<class name="Role" table="itcast_role">
		<id name="id">
            <generator class="native"/>
		</id>
		<property name="name" />
		<property name="description" />
		
		
		<!-- users属性,本类与User的多对多,中间表维系多对多关系
		     table:中间表

		 -->
		<set name="users" table="itcast_user_role">
			<!--本类在User中的外键-->
			<key column="roleId"></key>
			<!--
				 class:users所属的类型
				 column:User在本类中的外键
			-->
			<many-to-many class="User" column="userId"></many-to-many>
		</set>
		
	</class>
	
</hibernate-mapping>


二、在以下的实体设计中将涉及到特殊的多对一配置:主要涉及Article、Forum、Topic、Replay四个实体类



1、Forum实体

package cn.itcast.oa.domain;

import java.util.HashSet;
import java.util.Set;

/**
 * 版块
 * 
 * @author tyg
 * 
 */
public class Forum {
	private Long id;
	private String name;//包块名称
	private String description;//版块描述
	private int position; // 排序用的位置号

	private Set<Topic> topics = new HashSet<Topic>();
	private int topicCount; // 主题数量
	private int articleCount; // 文章数量(主题+回复)
	private Topic lastTopic; // 最后发表的主题

    setter and getter......
}
Forum实体配置文件:Forum.hbm.xml

<?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="cn.itcast.oa.domain">

	<class name="Forum" table="itcast_forum">
		<id name="id">
			<generator class="native" />
		</id>
		<property name="name" />
		<property name="description" />
		<property name="position" />
		
		
		<property name="topicCount"></property>
		<property name="articleCount"></property>
		
		<!-- topics属性,本类与Topic的一对多 -->
		<set name="topics">
			<key column="forumId"></key>
			<one-to-many class="Topic"/>
		</set>
		
		
		<!-- <span style="color:#ff0000;">特殊的多对一:虽然是一对一,但是该属性属于单项关联,只在本类中,Topic没有与之的                     属性与之对应</span>
                         lastTopic属性,本类与Topic的一对一。
			采用基于外键的一对一映射方式,本方有外键。 -->
		<many-to-one name="lastTopic" class="Topic" column="lastTopicId" unique="true"></many-to-one>
		
		
	</class>

</hibernate-mapping>

2、 Article是Topic、Replay的父类,抽取Topic、Replay得共同属性

package cn.itcast.oa.domain;

import java.util.Date;

/**
 * 文章
 * 
 * @author tyg
 */
public class Article {
	private Long id;
	private String title; // 标题
	private String content;// 内容
	private User author;// 作者
	private Date postTime;// 发表时间
	private String ipAddr;// 发表文章时所用的IP地址

	setter and getter......
}

3、 Topic实体

package cn.itcast.oa.domain;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * 主题
 * 
 * @author tyg
 */
public class Topic <span style="color:#ff0000;">extends Article</span> {

	/** 普通帖 */
	public static final int TYPE_NORMAL = 0;

	/** 精华帖 */
	public static final int TYPE_BEST = 1;

	/** 置顶帖 */
	public static final int TYPE_TOP = 2;


	private Forum forum;// 所属版块
	private Set<Reply> replies = new HashSet<Reply>();  //回复

	private int type;// 类型
	private int replyCount;// 回复数量
	private Reply lastReply;// 最后回复
	private Date lastUpdateTime;// 最后更新时间(主题发表时间或最后回复的时间)

	setter and getter.....
}
Topic实体配置文件:Topic.hbm.xml

<?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="cn.itcast.oa.domain">

	<class name="Topic" table="itcast_topic">
		<id name="id">
			<generator class="native" />
		</id>
		<property name="title" />
		<property name="content" type="text" length="65536"/>
		<property name="postTime" type="timestamp" />
		<property name="ipAddr" />
		<!-- author属性,本类与User的多对一-->
		<many-to-one name="author" class="User" column="authorId"></many-to-one>


		<property name="type" />
		<property name="replyCount" />
		<property name="lastUpdateTime" type="timestamp" />


		<!-- forum属性,本类与Forum的多对一-->
		<many-to-one name="forum" class="Forum" column="forumId"></many-to-one>


		<!-- replies属性,本类与Reply的一对多-->
		<set name="replies">
			<key column="topicId"></key>
			<one-to-many class="Reply"/>
		</set>


		<!--<pre name="code" class="html" style="font-size: 18px; font-weight: bold;"><span style="color:#ff0000;"></span><pre name="code" class="html" style="font-size: 18px; font-weight: bold;"><pre name="code" class="html" style="font-size: 18px; font-weight: bold;"><span style="color:#ff0000;">                 特殊的多对一:虽然是一对一,但是该属性属于单项关联,只在本类中,Reply没有与之的                     属性与之对应</span>

 
 
 
                  lastReply属性,本类与Reply的一对一。
			采用基于外键的一对一映射,本方有外键。 -->
		<many-to-one name="lastReply" class="Reply" column="lastReplyId" unique="true"></many-to-one>


	</class>

</hibernate-mapping>


3、 Replay实体

package cn.itcast.oa.domain;

/**
 * 回复
 * 
 * @author
 */
public class Reply <span style="color:#ff0000;">extends Article</span> {
	private Topic topic; // 所属的主题

	public Topic getTopic() {
		return topic;
	}

	public void setTopic(Topic topic) {
		this.topic = topic;
	}
}

Replay实体配置文件:Reply.hbm.xml

<?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="cn.itcast.oa.domain">

	<class name="Reply" table="itcast_reply">
		<id name="id">
			<generator class="native" />
		</id>
		<property name="title" />
		<property name="content" type="text" length="65536"/>
		<property name="postTime" type="timestamp" />
		<property name="ipAddr" />
		<!-- author属性,本类与User的多对一-->
		<many-to-one name="author" class="User" column="authorId"></many-to-one>
		
		<!-- topic属性,本类与Topic的多对一-->
		<many-to-one name="topic" class="Topic" column="topicId"></many-to-one>
		
	</class>

</hibernate-mapping>






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值