Hibernate采用XML方式映射的方法

LetonLIU 2013-09-16

Hibernate实体映射的几个步骤

 1、写注释

格式为:?属性,表达的是本对象与?的?关系。

例:“department属性,本对象与Department的多对一”

2、拷模板:

多对一<many-to-one name=“” class=“” column=“”/>
一对多(Set)

<set name="">

        <key column=""></key>

        <one-to-many class=""/>

</set>

多对多(Set)

<set name="" table="">

        <key column=""></key>

        <many-to-many class="" column=""/>

</set>

3、填空:

name属性:属性名(注释中的第1问号)

class属性:关联的实体类型(注释中的第2个问号)

column属性:

<many-to-one column="..">:一般可以写成属性名加Id后缀,如属性为department,则column值写成departmentId。

一对多中的<key column="..">:从关联的对方(对方是多对一)映射中把column值拷贝过来。 

多对多中的<key column=“..”>:一般可以写成本对象的名加Id后缀,如本对象名为User,则写为userId。

多对多中的<many-to-many column=“..”>:一般可以写为关联对象的名称加Id后缀。

举例:

<?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="com.yingjun">

	<class name="Role" table="t_role">
		<id name="id">
            <generator class="native"/>
		</id>
		<property name="name"/>
		<property name="description"/>
		
		<!-- users属性,本类与User的多对多 -->
		<set name="users" table="t_user_role">
			<key column="roleId"></key>
			<many-to-many class="User" column="userId"></many-to-many>
		</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="com.yingjun">

	<class name="User" table="t_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="t_user_role">
			<key column="userId"></key>
			<many-to-many class="Role" column="roleId"></many-to-many>
		</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="com.yingjun">

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

		<!-- 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">
			<key column="parentId"></key>
			<one-to-many class="Department" />
		</set>

	</class>

</hibernate-mapping>

相关推荐

LetonLIU / 0评论 2020-05-29
东方咖啡屋 / 0评论 2020-01-06