Hibernate学习之property-ref

来源:互联网 发布:七秀成女捏脸数据网盘 编辑:程序博客网 时间:2024/04/23 14:47

主外键一对一双向关联的实现中

 <one-to-one name="owner" property-ref="unid"></one-to-oneproperty-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也就是前面说的Userunid属性则

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属性就不起作用了

 

待续。。。。。。。