[JAVAEE Study Notes] hibernate03: Multi-table operation details, cascade, link maintenance and exercises: add Contacts, javaeehibernate03
One-to-many | multiple-to-one
1. relationship expression
Table expressions
Expressions in entities
Expression in orm metadata
One-to-multiple
<! -- Set, one-to-multiple relationship, configure in the configuration file --> <! -- Name attribute: set attribute name column attribute: foreign key column name class attribute: complete Class name of the object associated with me --> <set name = "linkMens" inverse = "true" cascade = "delete"> <key column = "lkm_cust_id"> </key> <one-to-learn class = "LinkMan"/> </set>
Many-to-one
<! -- Many-to-one --> <! -- Name attribute: Reference attribute column attribute: foreign key column name class attribute: complete class name of the object associated with me --> <strong-to-one name = "customer" column = "lkm_cust_id" class = "Customer"> </strong-to-one>
2. Operation
Operation association property
// 3 operation Customer c = new Customer (); c. setCust_name ("Chuanzhi podcast"); LinkMan lm1 = new LinkMan (); lm1.setLkm _ name ("Li Fumin"); LinkMan lm2 = new LinkMan (); lm2.setLkm _ name ("Liu Yue Dong"); // expression one-to-many. The customer has multiple contacts. c. getLinkMens (). add (lm1); c. getLinkMens (). add (lm2); // specifies the target customer of the contact, lm1.setCustomer (c), lm2.setCustomer (c), and session. save (c); session. save (lm1); session. save (lm2 );
3. Advanced Operations
Cascade operations
<! -- Cascade operation: cascade save-update: cascade save update delete: cascade delete all: save-update + delete cascade operation: simplify operation. the purpose is to reduce the number of lines of code. -->
<set name="linkMens" inverse="true" cascade="delete" > <key column="lkm_cust_id" ></key> <one-to-many class="LinkMan" /> </set>
Conclusion: to simplify the operation, save-update must be used. delete is not recommended.
Link Maintenance
During storage, both parties maintain the foreign key relationship. The relationship is maintained twice, and redundant. Redundant maintenance relationship statements are clearly maintained by the customer.
<! -- Inverse property: whether to maintain the configuration relationship. true: the customer does not maintain the relationship. false (default): the customer maintains the relationship. inverse attribute: performance optimization. improves the performance of link maintenance. principle: No matter how you give up, one party must maintain the relationship. in a one-to-multiple relationship: One Party gives up. only one party can give up. multiple parties cannot give up. --> <set name = "linkMens" inverse = "true" cascade = "delete"> <key column = "lkm_cust_id"> </key> <one-to-learn class = "LinkMan"/> </set>
Ii. many-to-many
1. relationship expression
Table expressions
Object
Orm metadata
<! -- Multi-to-Multi-link expression --> <! -- Name: set attribute name table: configure the intermediate table name key |-column: foreign key, others reference the "my" foreign key column name class: which of the following is my multi-to-Multi-link column: foreign key. I reference the foreign key column name of another person --> <! -- Cascade operation: save-update: cascade save update delete: cascade delete all: cascade save update + cascade delete conclusion: cascade simplifies code writing. it does not matter if this attribute is used. we recommend that you only use save-update. it is too dangerous to use the delete operation. especially in many-to-many scenarios. not recommended. --> <set name = "roles" table = "sys_user_role" cascade = "save-update"> <key column = "user_id"> </key> <sequence-to-Sequence class = "Role" column = "role_id"> </Role-to-Role> </set>
2. Operation
Operation association property
// 3> User relationship u1.getRoles (). add (r1); u1.getRoles (). add (r2); u2.getRoles (). add (r1); u2.getRoles (). add (r2); // 4> role expression relationship r1.getUsers (). add (u1); r1.getUsers (). add (u2); r2.getUsers (). add (u1); r2.getUsers (). add (u2); // 5> call the Save method to Save the session once. save (u1); session. save (u2); session. save (r1); session. save (r2 );
3. Advanced Operations
Inverse attributes
<! -- Use the inverse attribute true: discard maintenance of foreign key relationship false (default): maintain link conclusion: in future development, if many-to-many relationship is encountered. you must select one party to discard the maintenance relationship. generally, it depends on the business direction. for example, when the employee is working hours, you must specify a role for the employee. in this case, the role is maintained by employees. roles do not need to maintain relationships with employees. discard maintenance role --> <set name = "users" table = "sys_user_role" inverse = "true"> <key column = "role_id"> </key> <role-- using class = "User" column = "user_id"> </role-to-role> </set>
Cascade attributes
<! -- Cascade operation: save-update: cascade save update delete: cascade delete all: cascade save update + cascade delete conclusion: cascade simplifies code writing. it does not matter if this attribute is used. we recommend that you only use save-update. it is too dangerous to use the delete operation. especially in many-to-many scenarios. not recommended. --> <set name = "roles" table = "sys_user_role" cascade = "save-update"> <key column = "user_id"> </key> <sequence-to-Sequence class = "Role" column = "role_id"> </Role-to-Role> </set>
3. Exercise: Add a contact
Servlet:
Protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// 1> get the parameter and encapsulate LinkMan lm = new LinkMan () in the LinkMan object; try {BeanUtils. populate (lm, request. getParameterMap ();} catch (IllegalAccessException e) {e. printStackTrace ();} catch (InvocationTargetException e) {e. printStackTrace ();} // 2> call the Service to save the LinkMan object lms. save (lm); // 3> redirect to the LinkMan list (404) response. sendRedirect (request. getContextPath () + "/ListLinkManServlet ");}
Service:
Private CustomerDao cd = new CustomerDaoImpl (); private LinkManDao lmd = new LinkManDaoImpl (); public void save (LinkMan lm) {// open the transaction HibernateUtils. getCurrentSession (). beginTransaction (); try {// 1 obtain the customer object Long cust_id = lm based on the customer id. getCust_id (); Customer c = cd. getById (cust_id); // 2 put the customer into LinkMan to express the relation lm. setCustomer (c); // 3 save LinkMan lmd. save (lm);} catch (Exception e) {e. printStackTrace (); // roll back the transaction HibernateUtils. getCurrentSession (). getTransaction (). rollback ();} // submit the transaction HibernateUtils. getCurrentSession (). getTransaction (). commit ();}
Dao:
Public void save (LinkMan lm) {// 1 get session Session session = HibernateUtils. getCurrentSession (); session. save (lm );}