Hibernate学习之property-ref
来源:互联网 发布:七秀成女捏脸数据网盘 编辑:程序博客网 时间:2024/04/23 14:47
主外键一对一双向关联的实现中
<one-to-one name="owner" property-ref="unid"></one-to-one> 的property-ref的真正作用一直困惑这我,Hibernate的官方开发文档说的也很笼统,难以理解。经过多次实验探索,终于发现了其中的奥秘。不知道它的的作用的朋友请跟我一期做下面的试验:
首先写一个User类和一个Account类:
public class User { private Long oid; private String uid; private String name; private Account act; private Address adr; private Long unid; } public class Account { private Long oid; private String actNo; private double bal; private User owner; }
get,set方法省略。其中User里的unid属性先不必管它的具体意义,后面的实验中会用到。
在写这两个类对应的映射文件:
<class name="User" table="t_ebank_user">
<id name="oid" column="OID" >
<generator class="native"></generator>
</id>
<property name="uid" column="USERID"/>
<property name="unid"/>
<property name="name" column="NAME" not-null="true" /> <many-to-one name="act" column="fid" cascade="all" unique="true"></many-to-one> </class> <class name="Account" table="t_ebank_act"> <id name="oid" column="OID" > <generator class="native"></generator> </id> <property name="actNo" column="ACTNO" unique="true" not-null="true"></property> <property name="bal" column="BALANCE" not-null="true" /> <property name="unid" unique="true" /> <one-to-one name="owner"></one-to-one> </class>
接下来往数据库里插入两对象
Account a = new Account("act-001", 3000.0,3L);
Address adr = new Address("466200","sz","ba");
保存之后,查询
String hql = "select a from Account a join a.owner "
+ "where a.actNo=?";
Account a = (Account) s.createQuery(hql)
.setString(0, "act-001")
.uniqueResult();
a.getOwner();
先查出Account对象,在通过getOwner()方法获得User对象,hibernate执行了如下SQL语句,
Hibernate:
select
account0_.OID as OID0_,
account0_.ACTNO as ACTNO0_,
account0_.BALANCE as BALANCE0_,
account0_.unid as unid0_
from
t_ebank_act account0_
inner join
t_ebank_user user1_
on account0_.OID=user1_.OID
where
account0_.ACTNO=?
接下来在Account.hbm.xml映射文件的<one-to-one>
标签里加入 property-ref="act">再执行得出如下SQL
Hibernate:
select
account0_.OID as OID0_,
account0_.ACTNO as ACTNO0_,
account0_.BALANCE as BALANCE0_,
account0_.unid as unid0_
from
t_ebank_act account0_
inner join
t_ebank_user user1_
on account0_.OID=user1_.fid
where
account0_.ACTNO=?
注意比较黄色背景一句,如果把property-ref的值该成unid也就是前面说的User里unid属性则
Hibernate:
select
account0_.OID as OID0_,
account0_.ACTNO as ACTNO0_,
account0_.BALANCE as BALANCE0_,
account0_.unid as unid0_
from
t_ebank_act account0_
inner join
t_ebank_user user1_
on account0_.OID=user1_.unid
where
account0_.ACTNO=?
以上实验表明property-ref指定的值(name)所对应的列(column)就是由本对象关联查询相关对象时的条件,不指定时默认是对方的主键如第一个SQL语句
如果HQL语句改成这样
String hql = "select u from User u join fetch u.act "
+ "where u.uid=?";
则property-ref属性就不起作用了
待续。。。。。。。
- Hibernate学习之property-ref
- Hibernate property-ref 学习
- Hibernate property-ref属性
- Hibernate property-ref属性
- Hibernate property-ref
- Hibernate property-ref属性
- hibernate标签 property-ref 属性
- [hibernate property-ref]非主键关联
- property-ref
- property-ref
- hibernate 雙向 <one-to-one>中的property-ref属性
- oracle学习之:REF CURSOR
- spring bean property ref
- Hibernate使用property-ref属性解决遗留数据库One To Many关系的问题。
- Hibernate中property-ref的使用,常用来解决遗留数据库One To Many关系
- Hibernate使用property-ref属性解决遗留数据库One To Many关系的问题。
- Hibernate中property-ref的使用,常用来解决遗留数据库One To Many关
- 关于hibernate中双向外键关联one-to-one的property-ref=的问题
- 解决断电后每次开机都自检
- 自己写的模拟调制与解调 matlab gui程序
- 自勉
- fd_set以及select和poll的用法-驱动程序的阻塞与非阻塞--ZT
- 正则表达式的总结
- Hibernate学习之property-ref
- 小布老师讲座笔记(五)
- 07年写的MATLAB gui,纪念一下,
- 线程示例,一段没一点意义的代码,自已也不知道拿来做什么,跟着书上抄的
- 07年写的,直方图 matlab gui
- IT人应从五个方面做职业规划
- matlab 火力发电1
- 解析 Java 类和对象的初始化过程
- matlab 火力发电2