2017-08-13 50 views
0

我想獲取具有mhId =「AAA」的用戶的id和名稱(mhId是表User_MonHoc中的一列,在User_MonHoc中,mhid + user_id是主鍵)。如何使用WHERE子句選擇多個表

表用戶:

<hibernate-mapping> 
<class name="entites.User" table="user" catalog="bthibernate" optimistic-lock="version"> 
    <id name="username" type="string"> 
     <column name="username" length="100" /> 
     <generator class="assigned" /> 
    </id> 
    <property name="password" type="string"> 
     <column name="password" length="100" not-null="true" /> 
    </property> 
    <property name="note" type="byte"> 
     <column name="note" not-null="true" /> 
    </property> 
    <property name="name" type="string"> 
     <column name="name" length="100" not-null="true" /> 
    </property> 
    <set name="userMonhocs" table="user_monhoc" inverse="true" lazy="true" fetch="select"> 
     <key> 
      <column name="user_id" length="100" not-null="true" /> 
     </key> 
     <one-to-many class="entites.UserMonhoc" /> 
    </set> 
</class> 

表User_MonHoc

<hibernate-mapping> 
<class name="entites.UserMonhoc" table="user_monhoc" catalog="bthibernate" optimistic-lock="version"> 
    <composite-id name="id" class="entites.UserMonhocId"> 
     <key-property name="userId" type="string"> 
      <column name="user_id" length="100" /> 
     </key-property> 
     <key-property name="mhId" type="string"> 
      <column name="mh_id" length="100" /> 
     </key-property> 
    </composite-id> 
    <many-to-one name="monhoc" class="entites.Monhoc" update="false" insert="false" fetch="select"> 
     <column name="mh_id" length="100" not-null="true" /> 
    </many-to-one> 
    <many-to-one name="user" class="entites.User" update="false" insert="false" fetch="select"> 
     <column name="user_id" length="100" not-null="true" /> 
    </many-to-one> 
    <property name="thoigianDk" type="timestamp"> 
     <column name="thoigian_dk" length="19" not-null="true" /> 
    </property> 
</class> 

我想ID和用戶名,誰擁有mhId = 「AAA」(mhId是表User_MH中的一列)。 謝謝!

回答

0

您應該在SQL或HQL查詢中加入User和User_MH表。設置別名User_MH在查詢O訪問User_MH表在where子句:

SQL:

SELECT tbl_user.useranme,tbl_user_mh.user_id FROM User AS tbl_user 
INNER JOIN User_MH AS tbl_user_mh ON tbl_user.id=tbl_user_mh.user_id 
WHERE tbl_user_mh.mhdl = 'AAA