Hibernate的一对一关系


placeholder image
admin 发布于:2009-09-08 18:06:00
阅读:loading

在hibernate的一对一里面,如果<one-to-one/>里面只有2个属性,name,class属性,它  默认情况下查询的是按连个表的主键关联查询数据。

在某些一对一关系中,如果需要用到外间关联的话,比如表的结构如:

用户信息表:

SysUser用户表:字段如下:usr_id,usr_name,usr_age

UsrPower用户能力表:字段如下:pow_id,usr_id,pow_info(这个表与用户表一对一的时候用usr_id关联)

在查询UsrPower级联查询SysUser表数据,条件是SysUser.usr_id = UsrPower.usr_id这种情况下配置的信息为:

多对一:

<many-to-one name="users" class="com.fy.analyse.yy.vo.SysUser" insert="false" update="false"   column="fuser_id" not-found="ignore"></many-to-one>

生成出来的SQL语句是:

    select

        sysuser0_.USR_ID as USR1_8_0_,

        sysuser0_.USR_Uname as USR2_8_0_,

        sysuser0_.USR_Password as USR3_8_0_,

        sysuser0_.USR_Mail as USR4_8_0_,

        sysuser0_.USR_LoginTime as USR5_8_0_,

        sysuser0_.USR_Status as USR6_8_0_,

        sysuser0_.USR_No as USR7_8_0_,

        sysuser0_.USR_Name as USR8_8_0_,

        sysuser0_.USR_Sex as USR9_8_0_,

        sysuser0_.USR_Gra_ID as USR10_8_0_,

        sysuser0_.USR_Cla_ID as USR11_8_0_,

        sysuser0_.USR_Date as USR12_8_0_,

        sysuser0_.USR_National as USR13_8_0_,

        sysuser0_.USR_Phone as USR14_8_0_,

        sysuser0_.USR_Msn as USR15_8_0_,

        sysuser0_.USR_Website as USR16_8_0_,

        sysuser0_.USR_Std_SclYear as USR17_8_0_,

        sysuser0_.USR_Std_Specialty as USR18_8_0_,

        sysuser0_.USR_Tch_Education as USR19_8_0_,

        sysuser0_.USR_Tch_Specialty as USR20_8_0_,

        sysuser0_.USR_Tch_Title as USR21_8_0_,

        sysuser0_.USR_Tch_Course as USR22_8_0_,

        sysuser0_.USR_UserStatus as USR23_8_0_

    from

        System.dbo.SYS_User sysuser0_

    where

        sysuser0_.USR_ID=?

生成的SQL语句就只有一个表的数据,但是在获取的时候确实是可以获取到另一个数据表的数据的,

保存下来,以后遇到了再看看。。。

 点赞


 发表评论

当前回复:作者

 评论列表


留言区