hibernate 关联映射(二)---------one to one

本贴最后更新于 2384 天前,其中的信息可能已经时移世易

一对一

唯一外键关联:

一对一关联关系也是很常见的关联关系,如部门--部门经理,汽车--车位,人--身份证等等。

  • 有两种策略可以实现一对一的关联映射
    *唯一外键关联:外键关联,本来是用于多对一的配置,但是如果加上唯一的限制之后,也可以用来表示一对一关联关系。

dcce3eeeeedb482a9e88660f399f578e.png

省略 get set
Departments.java:

public class Departmentsp {
	private int deptid;
	private String deptname;
	private Manager mgr;

Managers.java:

public class Managerst {
	private int marid;
	private String mgrname;
	private Departments mgr;

Department.hbm.xml

其实是多对一的特例:
采用标签指定多的一端 unique 为 true
限制多的一端的唯一性

<hibernate-mapping>
	<class name="base.oneTone.foreign.Department" table="DEPARTMENTS">
		<id name="deptId" column="DEPT_ID">
			<generator class="native"></generator>
		</id>
		<property name="deptname" column="DEPT_NAME"></property>
		<many-to-one name="mgr" class="base.oneTone.foreign.Manager" column="MGR_ID" unique="true"></many-to-one>
	</class>
</hibernate-mapping>

Manager.hbm.xml:

映射 1-1 关联关系:在对应的数据表中已经有外键了,当前持久化类使用 one-to-one 进行映射

没有外键的一端需要使用 one-to-one 节点,该节点使用 property-ref 属性指定使用被关联实体(Department)主键以外的字段(MGR_ID)作为关联字段

<hibernate-mapping>
	<class name="base.oneTone.foreign.Manager" table="MANAGERS">
		<id name="marId" column="MAN_ID">
			<generator class="native"></generator>
		</id>
		<property name="mgrname" column="MGR_NAME"></property>
		<one-to-one name="dept" class="base.oneTone.foreign.Department" property-ref="mgr"></one-to-one>
	</class>
</hibernate-mapping>

Hibernate.cfg.xml:

<mapping resource=_"base/oneTone/foreign/Manager.hbm.xml"_/>

 <mapping resource=_"base/oneTone/foreign/Department.hbm.xml"_/>

主键关联:

即让两个对象具有相同的主键值,一个表中的外键列即主键列,以表明它们之间的一一对应的关系;数据库表不会有额外的字段来维护它们之间的关系,仅通过表的主键来关联

960213684538416c8581b69e5adf3310.png
即:departments 表的主键即 managers 的主键

Vo 与上述一致

Department.hbm.xml:

<hibernate-mapping>
	<class name="base.oneTone.foreign.Department" table="DEPARTMENTS">
		<id name="deptId" column="DEPT_ID">
			<!-- 使用外键的方式生成主键 -->
			<generator class="foreign">
			<!--property 属性指定使用当哪个持久化类的哪一个主键生成表主键, 与 one to one 联用  -->
				<param name="property">mgr</param>
			</generator>			
		</id>
		<property name="deptname" column="DEPT_NAME"></property>
		<!-- 采用foreign主键生成器的一端使用one to one 节点映射关联属性 -->
		<!--  -->
		<one-to-one name="mgr" class="base.oneTone.foreign.Manager" constrained="true"></one-to-one>
	</class>
</hibernate-mapping>

property-ref=_"mgr" 不再需要,因为左外连接条件为主键相等,

<hibernate-mapping>
	<class name="base.oneTone.foreign.Manager" table="MANAGERS">
		<id name="marId" column="MAN_ID">
			<generator class="native"></generator>
		</id>
		<property name="mgrname" column="MGR_NAME"></property>
		<one-to-one name="dept" class="base.oneTone.foreign.Department"></one-to-one>
	</class>
</hibernate-mapping>

Hibernate.cfg.xml:

<mapping resource=_"base/oneTone/foreign/Manager.hbm.xml"_/>

 <mapping resource=_"base/oneTone/foreign/Department.hbm.xml"_/>
  • Java

    Java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由 Sun Microsystems 公司于 1995 年 5 月推出的。Java 技术具有卓越的通用性、高效性、平台移植性和安全性。

    3169 引用 • 8207 回帖 • 1 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...