One or one-to-many | Many-to-one
1. Relationship Expression
Expression in the table
Expression in an entity
Expression in ORM Metadata
One-to-many
<!--collections, one-to-many relationships, configuring in configuration Files - <!--Name property: Collection Property Name column property: Foreign key Column Name Class Property: The full class name of the object associated with me - <Setname= "Linkmens"Inverse= "true"Cascade= "Delete" > <Keycolumn= "lkm_cust_id" ></Key> <One-to-manyclass= "Linkman" /> </Set>
Many-to-one
<!--- <!-- Name property: Reference Property name Column property: Foreign key Column Name class Property: Object associated with me full class name-- <name= "Customer" column = "lkm_cust_id" class = "Customer" > </ Many-to-one >
2. Operation
Action Association Properties
//3 OperationCustomer C =NewCustomer (); C.setcust_name ("Wisdom Podcast"); Linkman LM1=NewLinkman (); Lm1.setlkm_name ("Lai Live Ming"); Linkman lm2=NewLinkman (); Lm2.setlkm_name ("Liuyueying East"); //Express one-to-many clients with multiple contactsC.getlinkmens (). Add (LM1); C.getlinkmens (). Add (LM2); //To express to the right , to which customer the contact belongs.Lm1.setcustomer (c); Lm2.setcustomer (c); Session.save (c); Session.save (LM1); Session.save (LM2);
3. Advanced operation
Cascade Operations
<!-- Cascade Operations: Cascade save-update: Cascade Save update Delete: Cascade delete All:save-update+delete cascade operations: Simplify the operation. The goal is to have less than two lines of code. -
<set name= "Linkmens" Inverse= "true" Cascade= "delete" > <key column=" lkm_cust_id "></key> <one-to-many class< Span style= "COLOR: #0000ff" >= "Linkman" /> </set>
Conclusion: Simplify the operation. Be sure to use, save-update, delete is not recommended.
Relationship Maintenance
On save. Two parties maintain foreign key relationships. Relationship Maintenance two times, redundant. Redundant maintenance relationship statements, apparently the customer is maintaining the relationship at the end of the
<!--Inverse property: Configures whether the relationship is maintained. True:customer does not maintain relationship false (default): Customer maintains relationship inverse property: performance optimization. Improve the performance of relationship maintenance. Principle: No matter how to give up, there must always be a party to maintain the relationship. In a one-to-many relationship: a party gives up. Only one side can give up. Many of the parties cannot give up. - <Setname= "Linkmens"Inverse= "true"Cascade= "Delete" > <Keycolumn= "lkm_cust_id" ></Key> <One-to-manyclass= "Linkman" /> </Set>
Two, many-to-many
1. Relationship Expression
Expression in the table
Object in
ORM Meta-Data
<!--Many-to-many relationship expressions - <!--Name: Collection Property Name table: Configure the intermediate table name key |-column: Foreign Key, others refer to the Foreign key column name of "I" clas S: Which class I am with is a many-to-many relationship column: foreign key. I quote a foreign key column name than a person - <!--CASCADE Cascade Operations: Save-update: Cascade Save Update Delete: Cascade Delete all: Cascade Save update + level Union Delete Conclusion: Cascade simplifies code writing. This property makes no use of the matter. It is recommended to use only save-update. If using the delete operation is too dangerous. Especially in many-to-many. Not recommended. - <Setname= "Roles"Table= "Sys_user_role"Cascade= "Save-update" > <Keycolumn= "user_id" ></Key> <Many-to-manyclass= "Role"column= "role_id" ></Many-to-many> </Set>
2. Operation
Action Association Properties
// 3> User Expression Relationships U1.getroles (). Add (R1); U1.getroles (). Add (R2); U2.getroles (). Add (R1); U2.getroles (). Add (R2); // 4> Role Expression Relationships r1.getusers (). Add (U1); R1.getusers (). Add (U2); R2.getusers (). Add (U1); R2.getusers (). Add (U2); // 5> Call the Save method once save Session.save (U1); Session.save (U2); Session.save (R1); Session.save (R2);
3. Operation Advanced
Inverse property
<!--Use the Inverse property true: Discard the maintain foreign key relationship false (default): Maintain relationship Conclusion: in the future, if you encounter many-to-many relationships in development. Be sure to choose one side to abandon maintenance off General who will give up to see the business direction. For example, when entering an employee, you need to assign a role to the employee. Then the business direction is the employee maintenance role. Roles do not require maintenance and employee relationships. Role Abandonment Maintenance - <Setname= "Users"Table= "Sys_user_role"Inverse= "true" > <Keycolumn= "role_id" ></Key> <Many-to-manyclass= "User"column= "user_id" ></Many-to-many> </Set>
Cascading properties
<!--CASCADE Cascade Operations: Save-update: Cascade Save Update Delete: Cascade Delete all: Cascade Save update + level Union Delete Conclusion: Cascade simplifies code writing. This property makes no use of the matter. It is recommended to use only save-update. If using the delete operation is too dangerous. Especially in many-to-many. Not recommended. - <Setname= "Roles"Table= "Sys_user_role"Cascade= "Save-update" > <Keycolumn= "user_id" ></Key> <Many-to-manyclass= "Role"column= "role_id" ></Many-to-many> </Set>
Iii. Exercises: Adding contacts
Servlet:
protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {//1> Gets the parameters and encapsulates the Linkman objectLinkman LM =NewLinkman (); Try{beanutils.populate (LM, Request.getparametermap ()); } Catch(illegalaccessexception e) {e.printstacktrace (); } Catch(InvocationTargetException e) {e.printstacktrace (); } //2> Call service to save Linkman objectLMS. Save (LM); //3> Redirect to Linkman list (404)Response.sendredirect (Request.getcontextpath () + "/listlinkmanservlet"); }
Service
PrivateCustomerdao CD =NewCustomerdaoimpl (); PrivateLinkmandao LMD =NewLinkmandaoimpl (); Public voidSave (Linkman lm) {//Open Transactionhibernateutils.getcurrentsession (). BeginTransaction (); Try { //1 obtaining Customer objects based on customer IDLong cust_id =lm.getcust_id (); Customer C=Cd.getbyid (cust_id); //2 Put the customer into Linkman to express the relationshipLm.setcustomer (c); //3 Save LinkmanLmd.save (LM); } Catch(Exception e) {e.printstacktrace (); //rolling back a transactionhibernateutils.getcurrentsession (). Gettransaction (). rollback (); } //Commit a transactionhibernateutils.getcurrentsession (). Gettransaction (). commit (); }
Dao
Public void Save (Linkman lm) { ///1 GetSession Session Session = Hibernateutils.getcurrentsession (); Session.save (LM); }
Java EE Learning Note hibernate03: Multi-table operations, cascading exercises: adding contacts