Data Association mappings for hibernate

Source: Internet
Author: User
Tags generator

Data Association mapping is divided into: one-to-many, many-to-a, single-to-many.

Cascade Property
save-update : cascade operation when saving and modifying is performed
delete : Cascade operations when performing a delete
all cascade operations in all cases
none : Do not cascade operations (default)

Crawl strategy

One-to-many, many-to-

one-to-many, multi-pair persistence class design//User Class Public classUser {Private intID;PrivateString name;Private intAge ;PrivateSet<address>AddressSet; //Get Set Method}//Address class Public classAddress {Private intID;PrivateString address;Private intTel;Privateuser User; //Get Set Method}
Address.hbm.xml<hibernate-mapping Package= "Com.hussion.pojo">     <classname= "Address"Table= "T_address">         <IDname= "id">           <Generatorclass= "Native"/>         </ID>         < Propertyname= "Address"></ Property>         < Propertyname= "Tel"></ Property>         <Many-to-onename= "User"class= "User"column= "userid"/>     </class> </hibernate-mapping>
User.hbm.xml<hibernate-mapping Package= "Com.hussion.pojo">      <classname= "User"Table= "T_user">         <IDname= "id"column= "id">           <Generatorclass= "Native"></Generator>         </ID>         < Propertyname= "Name"column= "Name"></ Property>         < Propertyname= "Age"column= "Age"></ Property>         <!--inverse= "true" discards relationship maintenance -         <Setname= "AddressSet"Inverse= "true">           <Keycolumn= "userid"/>           <One-to-manyclass= "Address"/>         </Set>     </class>  </hibernate-mapping>

One

Persistence class Design Public classPerson {Private intID;PrivateString UserName;PrivateCard Card; //Get Set Method}  Public classCard {Private intID;PrivateString Cardnum;Privateperson person ; //Get Set Method}
Person.hbm.xml<hibernate-mapping Package= "Com.hussion.pojo">     <classname= "Person"Table= "T_person">         <IDname= "id">           <Generatorclass= "Native"></Generator>         </ID>          < Propertyname= "UserName"></ Property>           <one-to-onename= "card"class= "Card"Cascade= "Delete"/>    </class> </hibernate-mapping>

Many-to-many

UblicclassTeacher {Private intID;PrivateString name;PrivateSet<student>student; //Get Set Method}  Public classStudent {Private intID;PrivateString name;PrivateSet<teacher>teacher; //Get Set Method}
Teacher.hbm.xml<hibernate-mapping Package= "Com.hussion.pojo">     <classname= "Teacher"Table= "T_teacher">         <IDname= "id"column= "id">           <Generatorclass= "Native"></Generator>         </ID>           < Propertyname= "Name"></ Property>         <Setname= "Student"Table= "T_student_techer">           <Keycolumn= "Teacherid"/>           <Many-to-manyclass= "Student"column= "StudentID"/></set>     </class> </hibernate-mapping>
Student.hbm.xml<hibernate-mapping Package= "Com.hussion.pojo">     <classname= "Student"Table= "T_student">         <IDname= "id"column= "id">           <Generatorclass= "Native"></Generator>         </ID>         < Propertyname= "Name"></ Property>         <Setname= "Teacher"Table= "T_student_techer">             <Keycolumn= "StudentID"/>             <Many-to-manyclass= "Teacher"column= "Teacherid/>         </Set>     </class> </hibernate-mapping>

Many-to-many side to abandon relationship maintenance<Setname= "Student"Table= "T_teacher_student"Inverse= "true">Sort<Many-to-manyclass= "Student"column= "s_id"order-by= "ID asc,name desc"></Many-to-many>

Delayed load shutdown: lazy= "false"

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.