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"