hibernate 关系映射文件配置

Posted ljbguanli

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hibernate 关系映射文件配置相关的知识,希望对你有一定的参考价值。

<!--Department.hbm.xml users属性,本类与User的一对多 -->
		<set name="users">
			<key column="departmentId"></key>
			<one-to-many class="User" />
		</set>




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




		<!-- children属性,本类与Department(下级)的一对多 -->
		<set name="children" cascade="delete">  <!-- 级联删除 -->
			<key column="parentId"></key>
			<one-to-many class="Department" />
		</set>

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="qust_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的多对多 -->
		<set name="roles" table="itcast_user_role">
			<key column="userId"></key>
			<many-to-many class="Role" column="roleId"></many-to-many>
		</set>
		
		
	</class>
	
</hibernate-mapping>
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="User" table="qust_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的多对多 -->
		<set name="roles" table="itcast_user_role">
			<key column="userId"></key>
			<many-to-many class="Role" column="roleId"></many-to-many>
		</set>
		
		
	</class>
	
</hibernate-mapping>
所遵循的规律:
<!-- users属性,本类与User的一对多 -->
格式:?属性,本类与?的?
?1 属性名
?2 关联对类型
?3 关系


多对一:
<many-to-one name="" class="" column=""></many-to-one>
一对多(Set):
<set name="">
<key column=""></key>
<one-to-many class=""/>
</set>
多对多(Set):
<set name="" table="">
<key column=""></key>
<many-to-many class="" column=""></many-to-many>
</set>























以上是关于hibernate 关系映射文件配置的主要内容,如果未能解决你的问题,请参考以下文章

Hibernate5.x表与表之间的关系操作代码实现

hibernate笔记--使用注解(annotation)方式配置单(双)向多对一的映射关系

Hibernate的映射文件配置

hibernate 关系映射文件配置

javaEE 用户部门角色权限实体的关系设计与hibernate映射配置文件关系总结

Hibernate入门----配置文件