one-to-one在hibernate中可以用来作为两张表之间的主键关联,这也是hibernate中主键关联的一种用法,这样在一张表中的ID,在生成另外一张表的同时回自动插入到相应的ID字段中去,相应的XML文件设置比较简单,举例如下:
<!-- 建立一对一的到Address的映射,这个是写在User的XML配置文件中的 -->
<!-- 相应的User bean(PO)中也要添加属性 com.xx.Address address-->
<one-to-one name="address" cascade="all" class="com.xx.Address"/>
<!-- cascade的属性设置不再重复了,可以查看hibernate文档 -->
<!-- 建立一对一的到User的映射,这个是写在Address的XML配置文件中的 -->
<!-- 相应的Address bean(PO)中也要添加属性 com.xx.User user--> -->
<one-to-one name="user" class="com.xx.User" constrained="true"/>
为了在Address中使用User中的主键ID值,我们需要设置Address中的主键生成规则,如下所示,采用foreign关键字
<id column="ID" name="id" type="long" unsaved-value="0">
<generator class="foreign">
<param name="property">user</param>
</generator>
</id>
这里需要注意的是property的属性值必须与上面到User的映射所填写的name属性值一致,这样就完成了one-to-one的映射关系。
上面的过程都很简单,下面我来说说这里需要注意的地方:
1. 在设置属性ID的时候必须注意字段的长度,如笔者这样使用oracle的sequence来生成ID,其长度有14位之长,则应选择hibernate类型long,对应的实体中应选择Long,这样不会出现溢出的情况。
2. 在测试的时候必须要注意这两张表之间因为已经存在了一对一的关系,所以我们不能只写
user.setAddress(address);
而忽略了
address.setUser(user);
这样在做插入的时候会报出attempted to assign id from null one-to-one property: address的错误,这一点初学者会经常犯,笔者也是其中之一。
3. 如果不写cascade="all"或者写成cascade="none"的话,即使你写了
user.setAddress(address);
address.setUser(user);
也不会发生任何事情,只有user会被存储。
4. one-to-one的效率问题-------one-to-one在查询时,总是查出和主表关联的表,而且one-to-one的lazy属性只有 false proxy no-proxy三种,没有true。outer-join="false"也只是徒增查询语句条数,把本来的一条sql语句变成多条。所以在one- to-one这种一对一的关系不是很强的情况下(one-to-one关系强即总是查出这所有的几个关联表),或者是在一张表中存在多个one-to- one的情况下,使用最好one-to-many来代替one-to-one。
上面的第二条我是犯了,不知道你有没有犯。。
相关推荐
Kernel panic - not syncing: Attempted to kill init 解决办法 开源成就技术;技术成就梦想
Attempted to assign to readonly property报错 起因:项目中写了个点击事件,增加动态效果,类似于转一转,rotate(360)。很简单的一个特效。 设置元素的transition:transform 1s。 然后动态改变元素的transfrom:...
在使用MyEclipse进行SVN提交时遇到错误:“Attempted to lock an already-locked dir”,具体表现为以下错误信息: ``` svn: Working copy 'D:/Program Files/MyEclipse 6.6flex/workspace/emis/WebRoot/emis/...
* Description: This is the Sample code to show the usage of EDSDK. * * * * * ******************************************************************************* * * * Written and developed by Camera ...
在Linux系统中,"Kernel panic(Attempted to kill init!)"是一个非常严重的错误提示,它意味着内核遇到了无法恢复的问题,通常会导致系统崩溃。这个错误通常发生在ARM架构的设备上,比如开发板、嵌入式系统或者...
will need to be sent to Microsoft as per the normal process. However, a log entry will be added to the normal BurnInTest log. - Changes to trace logging to reduce activity when trace logging is not...
本人最近在玩树莓派4B的时候,树莓派在烧录镜像之后,无法启动,一直卡死在end kernel panic not syncing: attempted to kill init! exitcode = 0x00000000b 环境 设备:树莓派4B 查找原因–树莓派官网 根据官网给...
attempted from the File Manager. The result is that the name is reported as 'not resolved' even if the name exists in LMHOSTS file (since the driver wasn't even allowed to open LMHOSTS file). ...
在使用Subversion(SVN)进行版本控制时,有时可能会遇到一个常见的错误提示:“org.apache.subversion.javahl.ClientException: Attempted to lock an already-locked dir”。这个错误通常表示某个目录已经被锁定,...
首先,当新建虚拟机时遇到“end kernel panic - not syncing: corrupted stack end detected inside scheduler”或“end kernel panic - not syncing: Attempted to kill init! exit code=0x0000000b”的错误,可以...
在IT行业中,尤其是在Linux系统管理领域,经常遇到安装软件包时遇到依赖冲突的问题。本文将详细探讨如何解决在CentOS 6.5上安装pdksh-5.2.14-36.el5.rpm时可能遇到的冲突,并提供一个有效的解决方案。...
We have attempted to make every algorithm accessible and interesting. To help you when you encounter unfamiliar or difficult algorithms, we describe each one in a step-bystep manner. We also provide ...
由于[WebKit问题#138038] [],在使用Object.create创建的对象上分配属性可能会导致错误TypeError: Attempted to assign to readonly property. 。 已知此错误会影响iOS 8用户,尤其是在[Angular.js] []的最新版本...
id -a root 显示用户所在组的所有组名(如root用户,是所有组的组员) df 查看文件系统,查看数据区 用法 df [-F FSType] [-abeghklntVvZ] [-o FSType 特定选项] [目录 | 块设备 | 资源] df -k 以kbytes显示文件...
在配置Oracle 10g的ODBC数据源时,用户可能会遇到"an unsupported operation was attempted"的错误提示,这通常意味着系统尝试执行了一个不被当前环境支持的操作。 错误“an unsupported operation was attempted”...
如果 resultType 为基本类型,如 int,这时如果查询结果为空,MyBatis 会抛出异常:`org.apache.ibatis.binding.BindingException: Mapper method 'com.fkit.dao.xxDao.getUserById attempted to return null from a...
报错Connection to blog0@localhost failed. [08001] Could not create connection to database server. Attempted reconnect 3 times. Giving up. 解决办法:在url后面拼接?serverTimezone=UTC 连接成功后数据库...
这种情况下,系统会出现 "kernel panic-not syncing: VFS: Unable to mount root fs on unknown block" 错误信息。 二、解决方案 解决 CentOS 内核报错的方案是修改 grub.conf 文件,将默认启动的内核版本改为旧...
Even if you've never attempted a hardware project, this easy-to-follow book will guide you from your first blinking LED through connecting Arduino to your iPhone. About this Book Arduino in Action ...