Hibernate in a one-to-many association when the configuration of the Hbm.xml file

Source: Internet
Author: User

<title>Hibernate in a one-to-many association when the configuration of the Hbm.xml file</title>

Suppose there is a document manager class user, and a document class archives. Where user and archives are one-to-many bidirectional associations

Behave as follows in the class (the set and get methods of the property are omitted here)

The user class is defined as follows:

public   class  user  implements   Serializable   {/** definition ID */ private  Long ID; /** Define User name */ private  String username; /** define password */ private  String password; //defines the profile that the user wants to manage  private  List<archives> Archives;} 

The Archives class is defined as follows:

public   class  archives  implements  serializable   {private  static  final  long  serialversionuid = private  Long ID; //document name  private  String name; //define document manager  private  User Manager;}  
以上两个类分别映射到数据库中的User表和ARCHIVBES表
Configuration of the User.hbm.xml file:<!--Archives is the name of the document in the user class, whose type is list, and the name of the table in the corresponding database is archives.-->       <list name = "archives" table = "Archives" cascade = "  Save-update" lazy=" true ">       <!--The foreign key of the table associated with the user table, here is user_id of the archives table- -       <key column = "user_id" ></key>       <!--archives_id is the primary key in the ARCHIVES table--        <index column = "archives_id" ></Index>       <!--means that user and archives are one-to-many relationships--       <one-to-many class = "com.archivesManagement.domain.Archives" ></one-to-many>       </list> 
 archives.xml file configuration: <!--user_id for Archives foreign key-->  <many-to-one   "manager"  column  = "user_id "class  ="  Com.archivesManagement.domain.User  "not-null  =" true  ";  </many-to-one   
%0a%0a%u5047%u8bbe%u6709%u4e00%u4e2a%u6587%u6863%u7ba1%u7406%u8005%u7c7buser%2c%u548c%u4e00%u4e2a%u6587%u6863% U7c7barchives%u3002%u5176%u4e2duser%u548carchives%u662f%u4e00%u5bf9%u591a%u53cc%u5411%u5173%u8054%u5173%u7cfb %0a%0a%u8868%u73b0%u5728%u7c7b%u4e2d%u5982%u4e0b%28%u6b64%u5904%u7701%u7565%u4e86%u5c5e%u6027%u7684set% u548cget%u65b9%u6cd5%uff09%0a%0auser%u7c7b%u5b9a%u4e49%u5982%u4e0b%uff1a%0a%20%20%20%0a%20%20%20%20public% 20class%20user%20implements%20serializable%20%7b%0a%09/**%20%u5b9a%u4e49id%20*/%0a%20%20%20%20private%20long% 20id%3b%0a%20%20%20%20/**%20%u5b9a%u4e49%u7528%u6237%u540d*/%0a%20%20%20%20private%20string%20username%3b%0a% 20%20%20%20/**%20%u5b9a%u4e49%u5bc6%u7801*/%0a%20%20%20%20private%20string%20password%3b%0a%09//%u5b9a%u4e49% u8be5%u7528%u6237%u8981%u7ba1%u7406%u7684%u6863%u6848%0a%20%20%20%20private%20list%3carchives%3e%20archives%3b %0a%20%20%20%20%7d%0a---------------------------------------------------------------------------------------%0 Aarchives%u7c7b%u5b9a%u4e49%u5982%u4e0b%uff1a%0a%20%20%20%20%0a%20%20%20%20public%20class%20archives%20implements%20serializable%20%7b %0a%09private%20static%20final%20long%20serialversionuid%20%3d%201l%3b%0a%09private%20long%20id%3b%0a%09//%20% u6587%u6863%u540d%u5b57%0a%09private%20string%20name%3b%0a%09//%20%u5b9a%u4e49%u6587%u6863%u7ba1%u7406%u8005%0 a%09private%20user%20manager%3b%0a%09%7d%0a%09%0 A---------------------------------------------------------------------------------------%0a%0a%20%20%20%20% u4ee5%u4e0a%u4e24%u4e2a%u7c7b%u5206%u522b%u6620%u5c04%u5230%u6570%u636e%u5e93%u4e2d%u7684user%u8868% u548carchivbes%u8868%0a%20%20%20%20%0 A---------------------------------------------------------------------------------------%0a%20%20%20%20%0a%20% 20%20%20user.hbm.xml%u6587%u4ef6%u7684%u914d%u7f6e%uff1a%0a%0a%20%20%20%20%20%20%20%20%20%20%20%20%3c%21-- archives%u662fuser%u7c7b%u91cc%u6587%u6863%u7684%u540d%u5b57%uff0c%u5b83%u7684%u7c7b%u578b%u662flist%uff0c% U4ed6%u6240%u5bf9%u5e94%u7684%u6570%u636e%u5e93%u4e2d%u7684%u8868%u7684%0a%20%20%20%20%20%20%20%20%20%20%20%20%u540d%u5b57%u662farchives.--%3e%0a%20%20%20%20%20%20% 20%20%20%20%20%3clist%20name%20%3d%20%22archives%22%20%20table%20%3d%20%22archives%22%20cascade%20%3d%20% 22save-update%22%20%20lazy%3d%22true%22%3e%0a%09%09%20%20%20%3c%21--%20%u7528%u6237%u8868%u6240%u5173%u8054% u7684%u8868%u7684%u5916%u952e%uff0c%u6b64%u5904%u4e3aarchives%u8868%u7684user_id%20--%3e%0a%09%09%20%20%20% 3ckey%20column%20%3d%20%22user_id%22%3e%3c/key%3e%0a%09%09%20%20%20%3c%21--%20archives_id%u662farchives%u8868% u4e2d%u7684%u4e3b%u952e%20--%3e%0a%09%09%20%20%20%20%3cindex%20column%20%3d%20%22archives_id%22%3e%3c/index%3e %0a%09%09%20%20%20%3c%21--%20%u8868%u793auser%u548carchives%u662f%u4e00%u5bf9%u591a%u5173%u7cfb%20--%3e%0a%09 %09%20%20%20%3cone-to-many%20class%20%3d%20%22com.archivesmanagement.domain.archives%22%3e%3c/one-to-many%3e%0 a%09%09%20%20%20%3c/list%3e%20%0a%09%09%20%20%20%0 A---------------------------------------------------------------------------------------%0a%0a%20%20%20%20%20%20%0a%20%20%20%20%20archives.xml%u6587%u4ef6%u7684%u914d%u7f6e%uff1a%0a%20%20%20% 20%20%3c%21--%20user_id%u4e3aarchives%u7684%u5916%u952e%20--%3e%0a%09%09%3cmany-to-one%20name%3d%22manager%22 %09%09%20%20%0a%09%09%20%20column%20%3d%20%22user_id%22%0a%09%09%20%20class%20%3d%20% 22com.archivesmanagement.domain.user%22%0a%09%09%20%20not-null%20%3d%20%22true%22%3e%0a%09%20%20%20%20%3c/ many-to-one%3e%0a%09%09


From for notes (Wiz)

Hibernate in a one-to-many association when the configuration of the Hbm.xml file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.