hibernate關聯關係查詢

來源:互聯網
上載者:User

標籤:property   多對一   mod   uid   res   generator   hbm   ati   初始化   

關聯關係

一對一 A中包含B的對象,B中包含A的對象

一對多 A中包含B的集合,B中包含A的對象

多對多 A中包含B的集合,B中包含A的集合

1,一對多配置

  一名老師可以對應多名學生

2,模型類

    老師類中包含學生的集合(通常可以將集合直接進行初始化)

    學生類中包含老師的對象

3,配置

  

 1 TeacherModel.hbm.xml(教師) 2                 <hibernate-mapping package="cn.itcast.h3.realtion.one2many.vo" > 3                     <class name="TeacherModel" table="tbl_teacher"> 4                         <id name="uuid"> 5                             <generator class="native"/> 6                         </id> 7                         <property name="teacherName"/> 8                         <property name="nick"/> 9                         <!-- name:集合對象名 -->10             <set name="students">11                 <!-- 使用key描述對應的關係表中的外鍵欄位 -->12                 <!-- column:外鍵欄位名(student表中) -->13                 <key column="teacherUuid"/>14                 <!-- 使用one-to-many配置關聯關係 -->15                 <!-- class:配置對應的關聯式模式類 -->16                 <one-to-many class="cn.itcast.h3.one2many.StudentModel"/>17                         </set>18                     </class>19                 </hibernate-mapping>20             注意:key元素必須出現在one-to-many元素的前面,否則報錯21             StudentModel.hbm.xml(學生)22                 <hibernate-mapping package="cn.itcast.h3.realtion.one2many.vo" >23                     <class name="StudentModel" table="tbl_student">24                         <id name="uuid">25                             <generator class="native"/>26                         </id>27                         <property name="studentName"/>28                         <property name="className"/>29                            <!-- 配置多對一關聯性中的一方對象 -->30                         <!-- many-to-one描述多對一關聯性 -->31                         <!-- name:對象名 -->32                         <!-- column:外鍵欄位名(student表中) -->33                         <!-- class:配置對應的關聯式模式類 -->34                         <many-to-one 35                             name="teacher"36                             column="teacherUuid"37                             class="cn.itcast.h3.one2many.TeacherModel"38                             />39                     </class>40                 </hibernate-mapping>

4,資源註冊  

<mapping resource="cn/itcast/h3/one2many/vo/StudentModel.hbm.xml"/>               <mapping resource="cn/itcast/h3/one2many/vo/TeacherModel.hbm.xml"/>

5,備忘:

  兩邊的配置也可以只配置一方,

  如果僅僅配置老師可以通過老師找到學生,但是不能通過學生找到老師

  如果只配置學生可以通過學生找到老師,但是不能通過老師找到學生

  

 

 

  

hibernate關聯關係查詢

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.